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-plugin-filesystem): throw meaningful errors on bad inputs #10123

Merged
merged 1 commit into from
Nov 26, 2018
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const createRemoteFileNode = require(`../create-remote-file-node`)

describe(`create-remote-file-node`, () => {
const defaultArgs = {
url: ``,
store: {},
cache: {},
createNode: jest.fn(),
createNodeId: jest.fn(),
}

it(`throws on invalid inputs: createNode`, () => {
expect(() => {
createRemoteFileNode({
...defaultArgs,
createNode: undefined,
})
}).toThrowErrorMatchingInlineSnapshot(
`"createNode must be a function, was undefined"`
)
})

it(`throws on invalid inputs: createNodeId`, () => {
expect(() => {
createRemoteFileNode({
...defaultArgs,
createNodeId: undefined,
})
}).toThrowErrorMatchingInlineSnapshot(
`"createNodeId must be a function, was undefined"`
)
})

it(`throws on invalid inputs: cache`, () => {
expect(() => {
createRemoteFileNode({
...defaultArgs,
cache: undefined,
})
}).toThrowErrorMatchingInlineSnapshot(
`"cache must be the Gatsby cache, was undefined"`
)
})

it(`throws on invalid inputs: store`, () => {
expect(() => {
createRemoteFileNode({
...defaultArgs,
store: undefined,
})
}).toThrowErrorMatchingInlineSnapshot(
`"store must be the redux store, was undefined"`
)
})
})
18 changes: 18 additions & 0 deletions packages/gatsby-source-filesystem/src/create-remote-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ module.exports = ({
createNodeId,
ext = null,
}) => {
// validation of the input
// without this it's notoriously easy to pass in the wrong `createNodeId`
// see gatsbyjs/gatsby#6643
if (typeof createNodeId !== `function`) {
throw new Error(
`createNodeId must be a function, was ${typeof createNodeId}`
)
}
if (typeof createNode !== `function`) {
throw new Error(`createNode must be a function, was ${typeof createNode}`)
}
if (typeof store !== `object`) {
throw new Error(`store must be the redux store, was ${typeof store}`)
}
if (typeof cache !== `object`) {
throw new Error(`cache must be the Gatsby cache, was ${typeof cache}`)
}

// Check if we already requested node for this remote file
// and return stored promise if we did.
if (processingCache[url]) {
Expand Down