Skip to content

Commit

Permalink
Add error codes to reporter.panic in Contentful source plugin (#27457)
Browse files Browse the repository at this point in the history
* Add error codes to reporter.panic

* Backwards compat with no proxy

* Codes must be strings

* Update error tests

* Use native join instead of lodash

Co-authored-by: Peter van der Zee <209817+pvdz@users.noreply.github.com>

* prettier

Co-authored-by: Peter van der Zee <209817+pvdz@users.noreply.github.com>
  • Loading branch information
Sam Slotsky and pvdz authored Nov 6, 2020
1 parent 57b8023 commit ae2c3da
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 25 deletions.
72 changes: 59 additions & 13 deletions packages/gatsby-source-contentful/src/__tests__/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ it(`panics when localeFilter reduces locale list to 0`, async () => {
})

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(
`Please check if your localeFilter is configured properly. Locales 'en-us' were found but were filtered down to none.`
)
expect.objectContaining({
context: {
sourceMessage: `Please check if your localeFilter is configured properly. Locales 'en-us' were found but were filtered down to none.`,
},
})
)
})

Expand All @@ -200,11 +202,23 @@ describe(`Displays troubleshooting tips and detailed plugin options on contentfu
await fetchData({ pluginConfig, reporter })

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(`Accessing your Contentful space failed`)
expect.objectContaining({
context: {
sourceMessage: expect.stringContaining(
`Accessing your Contentful space failed`
),
},
})
)

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(`formatPluginOptionsForCLIMock`)
expect.objectContaining({
context: {
sourceMessage: expect.stringContaining(
`formatPluginOptionsForCLIMock`
),
},
})
)

expect(formatPluginOptionsForCLI).toBeCalledWith(
Expand All @@ -225,11 +239,21 @@ describe(`Displays troubleshooting tips and detailed plugin options on contentfu
await fetchData({ pluginConfig, reporter })

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(`You seem to be offline`)
expect.objectContaining({
context: {
sourceMessage: expect.stringContaining(`You seem to be offline`),
},
})
)

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(`formatPluginOptionsForCLIMock`)
expect.objectContaining({
context: {
sourceMessage: expect.stringContaining(
`formatPluginOptionsForCLIMock`
),
},
})
)

expect(formatPluginOptionsForCLI).toBeCalledWith(
Expand All @@ -250,11 +274,23 @@ describe(`Displays troubleshooting tips and detailed plugin options on contentfu
await fetchData({ pluginConfig, reporter })

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(`Check if host and spaceId settings are correct`)
expect.objectContaining({
context: {
sourceMessage: expect.stringContaining(
`Check if host and spaceId settings are correct`
),
},
})
)

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(`formatPluginOptionsForCLIMock`)
expect.objectContaining({
context: {
sourceMessage: expect.stringContaining(
`formatPluginOptionsForCLIMock`
),
},
})
)

expect(formatPluginOptionsForCLI).toBeCalledWith(
Expand All @@ -278,13 +314,23 @@ describe(`Displays troubleshooting tips and detailed plugin options on contentfu
await fetchData({ pluginConfig, reporter })

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(
`Check if accessToken and environment are correct`
)
expect.objectContaining({
context: {
sourceMessage: expect.stringContaining(
`Check if accessToken and environment are correct`
),
},
})
)

expect(reporter.panic).toBeCalledWith(
expect.stringContaining(`formatPluginOptionsForCLIMock`)
expect.objectContaining({
context: {
sourceMessage: expect.stringContaining(
`formatPluginOptionsForCLIMock`
),
},
})
)

expect(formatPluginOptionsForCLI).toBeCalledWith(
Expand Down
52 changes: 40 additions & 12 deletions packages/gatsby-source-contentful/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ const contentful = require(`contentful`)
const _ = require(`lodash`)
const chalk = require(`chalk`)
const { formatPluginOptionsForCLI } = require(`./plugin-options`)
const { CODES } = require(`./report`)

module.exports = async function contentfulFetch({
syncToken,
reporter,
pluginConfig,
reporter,
}) {
// Fetch articles.
const pageLimit = pluginConfig.get(`pageLimit`)
Expand Down Expand Up @@ -48,12 +49,14 @@ module.exports = async function contentfulFetch({
defaultLocale = _.find(contentfulLocales, { default: true }).code
locales = contentfulLocales.filter(pluginConfig.get(`localeFilter`))
if (locales.length === 0) {
reporter.panic(
`Please check if your localeFilter is configured properly. Locales '${_.join(
contentfulLocales.map(item => item.code),
`,`
)}' were found but were filtered down to none.`
)
reporter.panic({
id: CODES.LocalesMissing,
context: {
sourceMessage: `Please check if your localeFilter is configured properly. Locales '${contentfulLocales
.map(item => item.code)
.join(`,`)}' were found but were filtered down to none.`,
},
})
}
reporter.verbose(`Default locale is: ${defaultLocale}`)
} catch (e) {
Expand All @@ -63,7 +66,12 @@ module.exports = async function contentfulFetch({
details = `You seem to be offline`
} else if (e.code === `SELF_SIGNED_CERT_IN_CHAIN`) {
reporter.panic(
`We couldn't make a secure connection to your contentful space. Please check if you have any self-signed SSL certificates installed.`,
{
id: CODES.SelfSignedCertificate,
context: {
sourceMessage: `We couldn't make a secure connection to your contentful space. Please check if you have any self-signed SSL certificates installed.`,
},
},
e
)
} else if (e.response) {
Expand All @@ -88,11 +96,15 @@ module.exports = async function contentfulFetch({
}
}

reporter.panic(`Accessing your Contentful space failed.
reporter.panic({
context: {
sourceMessage: `Accessing your Contentful space failed.
Try setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache.
${details ? `\n${details}\n` : ``}
Used options:
${formatPluginOptionsForCLI(pluginConfig.getOriginalPluginOptions(), errors)}`)
${formatPluginOptionsForCLI(pluginConfig.getOriginalPluginOptions(), errors)}`,
},
})
}

let currentSyncData
Expand All @@ -106,7 +118,15 @@ ${formatPluginOptionsForCLI(pluginConfig.getOriginalPluginOptions(), errors)}`)
: { initial: true, ...basicSyncConfig }
currentSyncData = await client.sync(query)
} catch (e) {
reporter.panic(`Fetching contentful data failed`, e)
reporter.panic(
{
id: CODES.SyncError,
context: {
sourceMessage: `Fetching contentful data failed`,
},
},
e
)
}

// We need to fetch content types with the non-sync API as the sync API
Expand All @@ -115,7 +135,15 @@ ${formatPluginOptionsForCLI(pluginConfig.getOriginalPluginOptions(), errors)}`)
try {
contentTypes = await pagedGet(client, `getContentTypes`, pageLimit)
} catch (e) {
reporter.panic(`Error fetching content types`, e)
reporter.panic(
{
id: CODES.FetchContentTypes,
context: {
sourceMessage: `error fetching content types`,
},
},
e
)
}
reporter.verbose(`Content types fetched ${contentTypes.items.length}`)

Expand Down
31 changes: 31 additions & 0 deletions packages/gatsby-source-contentful/src/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const CODES = {
/* Fetch errors */

LocalesMissing: `111001`,
SelfSignedCertificate: `111002`,
SyncError: `111003`,
FetchContentTypes: `111004`,
}

export const ERROR_MAP = {
[CODES.LocalesMissing]: {
text: context => context.sourceMessage,
level: `ERROR`,
category: `USER`,
},
[CODES.SelfSignedCertificate]: {
text: context => context.sourceMessage,
level: `ERROR`,
category: `USER`,
},
[CODES.SyncError]: {
text: context => context.sourceMessage,
level: `ERROR`,
category: `THIRD_PARTY`,
},
[CODES.FetchContentTypes]: {
text: context => context.sourceMessage,
level: `ERROR`,
category: `THIRD_PARTY`,
},
}

0 comments on commit ae2c3da

Please sign in to comment.