From 2a5a330fa69876d67ea3f721f20f2161fa87cc25 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Mon, 29 Jan 2018 16:51:17 -0800 Subject: [PATCH] Improve errors for invalid data passed to createPage fixes #3771 --- .../__tests__/__snapshots__/pages.js.snap | 6 +++ packages/gatsby/src/redux/__tests__/pages.js | 31 +++++++++++++++ packages/gatsby/src/redux/actions.js | 39 +++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/packages/gatsby/src/redux/__tests__/__snapshots__/pages.js.snap b/packages/gatsby/src/redux/__tests__/__snapshots__/pages.js.snap index 903d664a64343..dc49f11a2a274 100644 --- a/packages/gatsby/src/redux/__tests__/__snapshots__/pages.js.snap +++ b/packages/gatsby/src/redux/__tests__/__snapshots__/pages.js.snap @@ -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 { diff --git a/packages/gatsby/src/redux/__tests__/pages.js b/packages/gatsby/src/redux/__tests__/pages.js index 404524586994c..34cff81f9aef8 100644 --- a/packages/gatsby/src/redux/__tests__/pages.js +++ b/packages/gatsby/src/redux/__tests__/pages.js @@ -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( { diff --git a/packages/gatsby/src/redux/actions.js b/packages/gatsby/src/redux/actions.js index 073cb18032b84..3e4a451ef32b6 100644 --- a/packages/gatsby/src/redux/actions.js +++ b/packages/gatsby/src/redux/actions.js @@ -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)}`