Skip to content

Commit 21ebf2c

Browse files
Haroenvpieh
authored andcommitted
fix(gatsby-plugin-filesystem): throw meaningful errors on bad inputs (#10123)
fixes #6643 I also fell for this error before, sinc the original errors are swallowed (and fairly unclear too, because of the indirection). Adding validation was suggested in linked issue, so I went for this, since I didn't see any already used object validation like "ow" in use here. <!-- Q. Which branch should I use for my pull request? A. Use `master` branch (probably). Q. Which branch if my change is a bug fix for Gatsby v1? A. In this case, you should use the `v1` branch Q. Which branch if I'm still not sure? A. Use `master` branch. Ask in the PR if you're not sure and a Gatsby maintainer will be happy to help :) Note: We will only accept bug fixes for Gatsby v1. New features should be added to Gatsby v2. Learn more about contributing: https://www.gatsbyjs.org/docs/how-to-contribute/ -->
1 parent 7325fd9 commit 21ebf2c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const createRemoteFileNode = require(`../create-remote-file-node`)
2+
3+
describe(`create-remote-file-node`, () => {
4+
const defaultArgs = {
5+
url: ``,
6+
store: {},
7+
cache: {},
8+
createNode: jest.fn(),
9+
createNodeId: jest.fn(),
10+
}
11+
12+
it(`throws on invalid inputs: createNode`, () => {
13+
expect(() => {
14+
createRemoteFileNode({
15+
...defaultArgs,
16+
createNode: undefined,
17+
})
18+
}).toThrowErrorMatchingInlineSnapshot(
19+
`"createNode must be a function, was undefined"`
20+
)
21+
})
22+
23+
it(`throws on invalid inputs: createNodeId`, () => {
24+
expect(() => {
25+
createRemoteFileNode({
26+
...defaultArgs,
27+
createNodeId: undefined,
28+
})
29+
}).toThrowErrorMatchingInlineSnapshot(
30+
`"createNodeId must be a function, was undefined"`
31+
)
32+
})
33+
34+
it(`throws on invalid inputs: cache`, () => {
35+
expect(() => {
36+
createRemoteFileNode({
37+
...defaultArgs,
38+
cache: undefined,
39+
})
40+
}).toThrowErrorMatchingInlineSnapshot(
41+
`"cache must be the Gatsby cache, was undefined"`
42+
)
43+
})
44+
45+
it(`throws on invalid inputs: store`, () => {
46+
expect(() => {
47+
createRemoteFileNode({
48+
...defaultArgs,
49+
store: undefined,
50+
})
51+
}).toThrowErrorMatchingInlineSnapshot(
52+
`"store must be the redux store, was undefined"`
53+
)
54+
})
55+
})

packages/gatsby-source-filesystem/src/create-remote-file-node.js

+18
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,24 @@ module.exports = ({
292292
createNodeId,
293293
ext = null,
294294
}) => {
295+
// validation of the input
296+
// without this it's notoriously easy to pass in the wrong `createNodeId`
297+
// see gatsbyjs/gatsby#6643
298+
if (typeof createNodeId !== `function`) {
299+
throw new Error(
300+
`createNodeId must be a function, was ${typeof createNodeId}`
301+
)
302+
}
303+
if (typeof createNode !== `function`) {
304+
throw new Error(`createNode must be a function, was ${typeof createNode}`)
305+
}
306+
if (typeof store !== `object`) {
307+
throw new Error(`store must be the redux store, was ${typeof store}`)
308+
}
309+
if (typeof cache !== `object`) {
310+
throw new Error(`cache must be the Gatsby cache, was ${typeof cache}`)
311+
}
312+
295313
// Check if we already requested node for this remote file
296314
// and return stored promise if we did.
297315
if (processingCache[url]) {

0 commit comments

Comments
 (0)