Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-source-filesystem,gatsby-transformer-sharp): Use custom errors #27576

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/gatsby-source-filesystem/src/error-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const CODES = {
Generic: `10000`,
MissingResource: `10001`,
}

export const pluginPrefix = `gatsby-source-filesystem`

export function prefixId(id) {
return `${pluginPrefix}_${id}`
}

// TODO: Refactor to use contextual data instead of only context.sourceMessage
// once reporter.setErrorMap is guaranteed to be available
export const ERROR_MAP = {
[CODES.Generic]: {
text: context => context.sourceMessage,
level: `ERROR`,
type: `PLUGIN`,
},
[CODES.MissingResource]: {
text: context => context.sourceMessage,
level: `ERROR`,
type: `PLUGIN`,
category: `USER`,
},
}
17 changes: 14 additions & 3 deletions packages/gatsby-source-filesystem/src/extend-file-node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const { GraphQLString } = require(`gatsby/graphql`)
const fs = require(`fs-extra`)
const path = require(`path`)
const { prefixId, CODES } = require(`./error-utils`)

module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = `` }) => {
module.exports = ({
type,
getNodeAndSavePathDependency,
pathPrefix = ``,
reporter,
}) => {
if (type.name !== `File`) {
return {}
}
Expand Down Expand Up @@ -30,8 +36,13 @@ module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = `` }) => {
{ dereference: true },
err => {
if (err) {
console.error(
`error copying file from ${details.absolutePath} to ${publicPath}`,
reporter.panic(
{
id: prefixId(CODES.MissingResource),
context: {
sourceMessage: `error copying file from ${details.absolutePath} to ${publicPath}`,
},
},
err
)
}
Expand Down
7 changes: 7 additions & 0 deletions packages/gatsby-source-filesystem/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ const path = require(`path`)
const { Machine, interpret } = require(`xstate`)

const { createFileNode } = require(`./create-file-node`)
const { ERROR_MAP } = require(`./error-utils`)

exports.onPreInit = ({ reporter }) => {
if (reporter.setErrorMap) {
reporter.setErrorMap(ERROR_MAP)
}
}

/**
* Create a state machine to manage Chokidar's not-ready/ready states.
Expand Down
10 changes: 8 additions & 2 deletions packages/gatsby-transformer-sharp/src/customize-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
PotraceType,
ImageFitType,
} = require(`./types`)
const { prefixId, CODES } = require(`./error-utils`)

function toArray(buf) {
const arr = new Array(buf.length)
Expand Down Expand Up @@ -441,8 +442,13 @@ const createFields = ({
// this is no longer in progress
inProgressCopy.delete(publicPath)
if (err) {
console.error(
`error copying file from ${details.absolutePath} to ${publicPath}`,
reporter.panic(
{
id: prefixId(CODES.MissingResource),
context: {
sourceMessage: `error copying file from ${details.absolutePath} to ${publicPath}`,
},
},
err
)
}
Expand Down
26 changes: 26 additions & 0 deletions packages/gatsby-transformer-sharp/src/error-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const CODES = {
Generic: `20000`,
MissingResource: `20001`,
}

export const pluginPrefix = `gatsby-transformer-sharp`

export function prefixId(id) {
return `${pluginPrefix}_${id}`
}

// TODO: Refactor to use contextual data instead of only context.sourceMessage
// once reporter.setErrorMap is guaranteed to be available
export const ERROR_MAP = {
[CODES.Generic]: {
text: context => context.sourceMessage,
level: `ERROR`,
type: `PLUGIN`,
},
[CODES.MissingResource]: {
text: context => context.sourceMessage,
level: `ERROR`,
type: `PLUGIN`,
category: `USER`,
},
}
7 changes: 7 additions & 0 deletions packages/gatsby-transformer-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ const {
onCreateNode,
unstable_shouldOnCreateNode,
} = require(`./on-node-create`)
const { ERROR_MAP } = require(`./error-utils`)

exports.onPreInit = ({ reporter }) => {
if (reporter.setErrorMap) {
reporter.setErrorMap(ERROR_MAP)
}
}
exports.onCreateNode = onCreateNode
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.createSchemaCustomization = require(`./customize-schema`)
Expand Down