Skip to content

Commit 752e27a

Browse files
authored
fix(gatsby-source-filesystem,gatsby-transformer-sharp): Use custom errors (#27576)
1 parent d6558b0 commit 752e27a

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const CODES = {
2+
Generic: `10000`,
3+
MissingResource: `10001`,
4+
}
5+
6+
export const pluginPrefix = `gatsby-source-filesystem`
7+
8+
export function prefixId(id) {
9+
return `${pluginPrefix}_${id}`
10+
}
11+
12+
// TODO: Refactor to use contextual data instead of only context.sourceMessage
13+
// once reporter.setErrorMap is guaranteed to be available
14+
export const ERROR_MAP = {
15+
[CODES.Generic]: {
16+
text: context => context.sourceMessage,
17+
level: `ERROR`,
18+
type: `PLUGIN`,
19+
},
20+
[CODES.MissingResource]: {
21+
text: context => context.sourceMessage,
22+
level: `ERROR`,
23+
type: `PLUGIN`,
24+
category: `USER`,
25+
},
26+
}

packages/gatsby-source-filesystem/src/extend-file-node.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
const { GraphQLString } = require(`gatsby/graphql`)
22
const fs = require(`fs-extra`)
33
const path = require(`path`)
4+
const { prefixId, CODES } = require(`./error-utils`)
45

5-
module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = `` }) => {
6+
module.exports = ({
7+
type,
8+
getNodeAndSavePathDependency,
9+
pathPrefix = ``,
10+
reporter,
11+
}) => {
612
if (type.name !== `File`) {
713
return {}
814
}
@@ -30,8 +36,13 @@ module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = `` }) => {
3036
{ dereference: true },
3137
err => {
3238
if (err) {
33-
console.error(
34-
`error copying file from ${details.absolutePath} to ${publicPath}`,
39+
reporter.panic(
40+
{
41+
id: prefixId(CODES.MissingResource),
42+
context: {
43+
sourceMessage: `error copying file from ${details.absolutePath} to ${publicPath}`,
44+
},
45+
},
3546
err
3647
)
3748
}

packages/gatsby-source-filesystem/src/gatsby-node.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ const path = require(`path`)
44
const { Machine, interpret } = require(`xstate`)
55

66
const { createFileNode } = require(`./create-file-node`)
7+
const { ERROR_MAP } = require(`./error-utils`)
8+
9+
exports.onPreInit = ({ reporter }) => {
10+
if (reporter.setErrorMap) {
11+
reporter.setErrorMap(ERROR_MAP)
12+
}
13+
}
714

815
/**
916
* Create a state machine to manage Chokidar's not-ready/ready states.

packages/gatsby-transformer-sharp/src/customize-schema.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
PotraceType,
3232
ImageFitType,
3333
} = require(`./types`)
34+
const { prefixId, CODES } = require(`./error-utils`)
3435

3536
function toArray(buf) {
3637
const arr = new Array(buf.length)
@@ -441,8 +442,13 @@ const createFields = ({
441442
// this is no longer in progress
442443
inProgressCopy.delete(publicPath)
443444
if (err) {
444-
console.error(
445-
`error copying file from ${details.absolutePath} to ${publicPath}`,
445+
reporter.panic(
446+
{
447+
id: prefixId(CODES.MissingResource),
448+
context: {
449+
sourceMessage: `error copying file from ${details.absolutePath} to ${publicPath}`,
450+
},
451+
},
446452
err
447453
)
448454
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const CODES = {
2+
Generic: `20000`,
3+
MissingResource: `20001`,
4+
}
5+
6+
export const pluginPrefix = `gatsby-transformer-sharp`
7+
8+
export function prefixId(id) {
9+
return `${pluginPrefix}_${id}`
10+
}
11+
12+
// TODO: Refactor to use contextual data instead of only context.sourceMessage
13+
// once reporter.setErrorMap is guaranteed to be available
14+
export const ERROR_MAP = {
15+
[CODES.Generic]: {
16+
text: context => context.sourceMessage,
17+
level: `ERROR`,
18+
type: `PLUGIN`,
19+
},
20+
[CODES.MissingResource]: {
21+
text: context => context.sourceMessage,
22+
level: `ERROR`,
23+
type: `PLUGIN`,
24+
category: `USER`,
25+
},
26+
}

packages/gatsby-transformer-sharp/src/gatsby-node.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ const {
22
onCreateNode,
33
unstable_shouldOnCreateNode,
44
} = require(`./on-node-create`)
5+
const { ERROR_MAP } = require(`./error-utils`)
6+
7+
exports.onPreInit = ({ reporter }) => {
8+
if (reporter.setErrorMap) {
9+
reporter.setErrorMap(ERROR_MAP)
10+
}
11+
}
512
exports.onCreateNode = onCreateNode
613
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
714
exports.createSchemaCustomization = require(`./customize-schema`)

0 commit comments

Comments
 (0)