Skip to content

Commit

Permalink
Improve errors for invalid data passed to createPage fixes #3771 (#3773)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews authored Jan 30, 2018
1 parent d025ae1 commit 9b98b68
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Add pages Fails if component path is missing 1`] = `"The plugin \\"test\\" must set the absolute path to the page component when create creating a page"`;

exports[`Add pages Fails if path is missing 1`] = `"The plugin \\"test\\" must set the page path when creating a page"`;

exports[`Add pages Fails if the component path isn't absolute 1`] = `"The plugin \\"test\\" must set the absolute path to the page component when create creating a page"`;

exports[`Add pages allows you to add multiple pages 1`] = `
Array [
Object {
Expand Down
31 changes: 31 additions & 0 deletions packages/gatsby/src/redux/__tests__/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@ describe(`Add pages`, () => {
expect(state).toMatchSnapshot()
})

it(`Fails if path is missing`, () => {
const action = actions.createPage(
{
component: `/whatever/index.js`,
},
{ id: `test`, name: `test` }
)
expect(action).toMatchSnapshot()
})

it(`Fails if component path is missing`, () => {
const action = actions.createPage(
{
path: `/whatever/`,
},
{ id: `test`, name: `test` }
)
expect(action).toMatchSnapshot()
})

it(`Fails if the component path isn't absolute`, () => {
const action = actions.createPage(
{
path: `/whatever/`,
component: `cheese.js`,
},
{ id: `test`, name: `test` }
)
expect(action).toMatchSnapshot()
})

it(`adds an initial forward slash if the user doesn't`, () => {
const action = actions.createPage(
{
Expand Down
39 changes: 39 additions & 0 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,45 @@ const pascalCase = _.flow(_.camelCase, _.upperFirst)
* })
*/
actions.createPage = (page: PageInput, plugin?: Plugin, traceId?: string) => {
let noPageOrComponent = false
let name = `The plugin "${plugin.name}"`
if (plugin.name === `default-site-plugin`) {
name = `Your site's "gatsby-node.js"`
}
if (!page.path) {
const message = `${name} must set the page path when creating a page`
// Don't log out when testing
if (!process.env.NODE_ENV === `test`) {
console.log(chalk.bold.red(message))
console.log(``)
console.log(page)
} else {
return message
}
noPageOrComponent = true
}

if (!page.component || !path.isAbsolute(page.component)) {
const message = `${name} must set the absolute path to the page component when create creating a page`
// Don't log out when testing
if (!process.env.NODE_ENV === `test`) {
console.log(chalk.bold.red(message))
console.log(``)
console.log(page)
} else {
return message
}
noPageOrComponent = true
}

if (noPageOrComponent) {
console.log(``)
console.log(
`See the documentation for createPage https://www.gatsbyjs.org/docs/bound-action-creators/#createPage`
)
process.exit(1)
}

let jsonName = `${_.kebabCase(page.path)}.json`
let internalComponentName = `Component${pascalCase(page.path)}`

Expand Down

0 comments on commit 9b98b68

Please sign in to comment.