diff --git a/packages/gatsby-source-filesystem/src/error-utils.js b/packages/gatsby-source-filesystem/src/error-utils.js new file mode 100644 index 0000000000000..8b65c4ce36bf2 --- /dev/null +++ b/packages/gatsby-source-filesystem/src/error-utils.js @@ -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`, + }, +} diff --git a/packages/gatsby-source-filesystem/src/extend-file-node.js b/packages/gatsby-source-filesystem/src/extend-file-node.js index 20433275b8952..1b430b1bf1a49 100644 --- a/packages/gatsby-source-filesystem/src/extend-file-node.js +++ b/packages/gatsby-source-filesystem/src/extend-file-node.js @@ -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 {} } @@ -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 ) } diff --git a/packages/gatsby-source-filesystem/src/gatsby-node.js b/packages/gatsby-source-filesystem/src/gatsby-node.js index 27713bb198cac..cb00044756f96 100644 --- a/packages/gatsby-source-filesystem/src/gatsby-node.js +++ b/packages/gatsby-source-filesystem/src/gatsby-node.js @@ -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. diff --git a/packages/gatsby-transformer-sharp/src/customize-schema.js b/packages/gatsby-transformer-sharp/src/customize-schema.js index 7ac669319b62a..5420e44592f6a 100644 --- a/packages/gatsby-transformer-sharp/src/customize-schema.js +++ b/packages/gatsby-transformer-sharp/src/customize-schema.js @@ -31,6 +31,7 @@ const { PotraceType, ImageFitType, } = require(`./types`) +const { prefixId, CODES } = require(`./error-utils`) function toArray(buf) { const arr = new Array(buf.length) @@ -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 ) } diff --git a/packages/gatsby-transformer-sharp/src/error-utils.js b/packages/gatsby-transformer-sharp/src/error-utils.js new file mode 100644 index 0000000000000..9a8f443f416a5 --- /dev/null +++ b/packages/gatsby-transformer-sharp/src/error-utils.js @@ -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`, + }, +} diff --git a/packages/gatsby-transformer-sharp/src/gatsby-node.js b/packages/gatsby-transformer-sharp/src/gatsby-node.js index 81be7827f5a02..0b32f5ab6b302 100644 --- a/packages/gatsby-transformer-sharp/src/gatsby-node.js +++ b/packages/gatsby-transformer-sharp/src/gatsby-node.js @@ -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`)