Skip to content

Commit

Permalink
fix(gatsby-source-contentful): Add panic messaging to builds with no …
Browse files Browse the repository at this point in the history
…locales (#21667)

* Add panic messaging to builds with no locales

* Add contentfulLocales to messaging
  • Loading branch information
joshuagagne authored Feb 23, 2020
1 parent f4295df commit 6979816
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/gatsby-source-contentful/src/__tests__/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ it(`calls contentful.getContentTypes with custom plugin option page limit`, asyn
})
})

it(`panics when localeFilter reduces locale list to 0`, async () => {
await fetchData({
pluginConfig: createPluginConfig({
accessToken: `6f35edf0db39085e9b9c19bd92943e4519c77e72c852d961968665f1324bfc94`,
spaceId: `rocybtov1ozk`,
pageLimit: 50,
localeFilter: () => false,
}),
reporter,
})

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.`
)
)
})

describe(`Displays troubleshooting tips and detailed plugin options on contentful client error`, () => {
it(`Generic fallback error`, async () => {
mockClient.getLocales.mockImplementation(() => {
Expand Down
16 changes: 13 additions & 3 deletions packages/gatsby-source-contentful/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ module.exports = async ({ syncToken, reporter, pluginConfig }) => {
try {
console.log(`Fetching default locale`)
space = await client.getSpace()
locales = await client.getLocales().then(response => response.items)
defaultLocale = _.find(locales, { default: true }).code
locales = locales.filter(pluginConfig.get(`localeFilter`))
let contentfulLocales = await client
.getLocales()
.then(response => response.items)
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.`
)
}
console.log(`default locale is : ${defaultLocale}`)
} catch (e) {
let details
Expand Down

0 comments on commit 6979816

Please sign in to comment.