Skip to content

Commit

Permalink
fix(gatsby-source-filesystem,gatsby-transformer-sharp): Use custom er…
Browse files Browse the repository at this point in the history
…rors (#27576)
  • Loading branch information
ax-vasquez authored Oct 26, 2020
1 parent d6558b0 commit 752e27a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 5 deletions.
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

0 comments on commit 752e27a

Please sign in to comment.