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

Improve errors for invalid data passed to createPage fixes #3771 #3773

Merged
merged 1 commit into from
Jan 30, 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
@@ -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