-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pretty print Contentful errors after all retry attempts failed
- Loading branch information
Showing
3 changed files
with
296 additions
and
29 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/gatsby-source-contentful/src/__tests__/__snapshots__/fetch-network-errors.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`download-with-retry fails after to many retries 1`] = ` | ||
"Fetching contentful data failed: ETIMEDOUT: | ||
The request was retried 6 times." | ||
`; | ||
|
||
exports[`download-with-retry properly prints api error with response string 1`] = ` | ||
"Accessing your Contentful space failed: Bad Gateway | ||
Try setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache. | ||
Used options: | ||
spaceId: [32m\\"****5\\"[39m | ||
accessToken: [32m\\"****0\\"[39m | ||
host: [32m\\"localhost\\"[39m | ||
contentfulClientConfig: [32m{\\"retryOnError\\":false}[39m | ||
environment[2m (default value)[22m: [2m\\"master\\"[22m | ||
downloadLocal[2m (default value)[22m: [2mfalse[22m | ||
localeFilter[2m (default value)[22m: [2m[Function][22m | ||
forceFullSync[2m (default value)[22m: [2mfalse[22m | ||
pageLimit[2m (default value)[22m: [2m100[22m | ||
useNameForId[2m (default value)[22m: [2mtrue[22m" | ||
`; | ||
|
||
exports[`download-with-retry properly prints network error with response object 1`] = ` | ||
"Accessing your Contentful space failed: RateLimitExceeded | ||
You have exceeded the rate limit of the Organization this Space belongs to by making too many API requests within a short timespan. Please wait a moment before trying the request again. | ||
Request ID: 123abc | ||
Try setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache. | ||
Used options: | ||
spaceId: [32m\\"****5\\"[39m | ||
accessToken: [32m\\"****0\\"[39m | ||
host: [32m\\"localhost\\"[39m | ||
contentfulClientConfig: [32m{\\"retryOnError\\":false}[39m | ||
environment[2m (default value)[22m: [2m\\"master\\"[22m | ||
downloadLocal[2m (default value)[22m: [2mfalse[22m | ||
localeFilter[2m (default value)[22m: [2m[Function][22m | ||
forceFullSync[2m (default value)[22m: [2mfalse[22m | ||
pageLimit[2m (default value)[22m: [2m100[22m | ||
useNameForId[2m (default value)[22m: [2mtrue[22m" | ||
`; | ||
|
||
exports[`download-with-retry properly prints plain network error 1`] = ` | ||
"Accessing your Contentful space failed: ETIMEDOUT: | ||
Try setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache. | ||
Used options: | ||
spaceId: [32m\\"****5\\"[39m | ||
accessToken: [32m\\"****0\\"[39m | ||
host: [32m\\"localhost\\"[39m | ||
contentfulClientConfig: [32m{\\"retryOnError\\":false}[39m | ||
environment[2m (default value)[22m: [2m\\"master\\"[22m | ||
downloadLocal[2m (default value)[22m: [2mfalse[22m | ||
localeFilter[2m (default value)[22m: [2m[Function][22m | ||
forceFullSync[2m (default value)[22m: [2mfalse[22m | ||
pageLimit[2m (default value)[22m: [2m100[22m | ||
useNameForId[2m (default value)[22m: [2mtrue[22m" | ||
`; |
182 changes: 182 additions & 0 deletions
182
packages/gatsby-source-contentful/src/__tests__/fetch-network-errors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import nock from "nock" | ||
import fetchData from "../fetch" | ||
import { createPluginConfig } from "../plugin-options" | ||
|
||
nock.disableNetConnect() | ||
|
||
const host = `localhost` | ||
const options = { | ||
spaceId: `12345`, | ||
accessToken: `67890`, | ||
host, | ||
} | ||
const baseURI = `https://${host}` | ||
|
||
const start = jest.fn() | ||
const end = jest.fn() | ||
const mockActivity = { | ||
start, | ||
end, | ||
tick: jest.fn(), | ||
done: end, | ||
} | ||
|
||
const reporter = { | ||
info: jest.fn(), | ||
verbose: jest.fn(), | ||
panic: jest.fn(e => { | ||
throw e | ||
}), | ||
activityTimer: jest.fn(() => mockActivity), | ||
createProgress: jest.fn(() => mockActivity), | ||
} | ||
|
||
const pluginConfig = createPluginConfig(options) | ||
|
||
describe(`download-with-retry`, () => { | ||
afterEach(() => { | ||
nock.cleanAll() | ||
reporter.verbose.mockClear() | ||
reporter.panic.mockClear() | ||
}) | ||
|
||
test(`retries on timeout`, async () => { | ||
const scope = nock(baseURI) | ||
// Space | ||
.get(`/spaces/${options.spaceId}/`) | ||
.reply(200, { items: [] }) | ||
// Locales | ||
.get(`/spaces/${options.spaceId}/environments/master/locales`) | ||
.reply(200, { items: [{ code: `en`, default: true }] }) | ||
// Sync | ||
.get( | ||
`/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=100` | ||
) | ||
.times(1) | ||
.replyWithError({ code: `ETIMEDOUT` }) | ||
.get( | ||
`/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=100` | ||
) | ||
.reply(200, { items: [] }) | ||
// Content types | ||
.get( | ||
`/spaces/${options.spaceId}/environments/master/content_types?skip=0&limit=100&order=sys.createdAt` | ||
) | ||
.reply(200, { items: [] }) | ||
|
||
await fetchData({ pluginConfig, reporter }) | ||
|
||
// expect(reporter.verbose).not.toHaveBeenCalled() | ||
expect(reporter.panic).not.toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
|
||
test(`fails after to many retries`, async () => { | ||
// Due to the retries, this will take 20+ seconds | ||
jest.setTimeout(30000) | ||
|
||
const scope = nock(baseURI) | ||
// Space | ||
.get(`/spaces/${options.spaceId}/`) | ||
.reply(200, { items: [] }) | ||
// Locales | ||
.get(`/spaces/${options.spaceId}/environments/master/locales`) | ||
.reply(200, { items: [{ code: `en`, default: true }] }) | ||
// Sync | ||
.get( | ||
`/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=100` | ||
) | ||
.times(5) | ||
.replyWithError({ code: `ETIMEDOUT` }) | ||
|
||
try { | ||
await fetchData({ pluginConfig, reporter }) | ||
jest.fail() | ||
} catch (e) { | ||
expect(e.context.sourceMessage).toMatchSnapshot() | ||
} | ||
|
||
// expect(reporter.verbose).not.toHaveBeenCalled() | ||
expect(reporter.panic).toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
|
||
test(`properly prints plain network error`, async () => { | ||
const scope = nock(baseURI) | ||
// Space | ||
.get(`/spaces/${options.spaceId}/`) | ||
.replyWithError({ code: `ETIMEDOUT` }) | ||
try { | ||
await fetchData({ | ||
pluginConfig: createPluginConfig({ | ||
...options, | ||
contentfulClientConfig: { retryOnError: false }, | ||
}), | ||
reporter, | ||
}) | ||
jest.fail() | ||
} catch (e) { | ||
expect(e.context.sourceMessage).toMatchSnapshot() | ||
} | ||
|
||
expect(reporter.panic).toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
|
||
test(`properly prints api error with response string`, async () => { | ||
const scope = nock(baseURI) | ||
// Space | ||
.get(`/spaces/${options.spaceId}/`) | ||
.reply(502, `Bad Gateway`) | ||
|
||
try { | ||
await fetchData({ | ||
pluginConfig: createPluginConfig({ | ||
...options, | ||
contentfulClientConfig: { retryOnError: false }, | ||
}), | ||
reporter, | ||
}) | ||
jest.fail() | ||
} catch (e) { | ||
expect(e.context.sourceMessage).toMatchSnapshot() | ||
} | ||
|
||
expect(reporter.panic).toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
|
||
test(`properly prints network error with response object`, async () => { | ||
const scope = nock(baseURI) | ||
// Space | ||
.get(`/spaces/${options.spaceId}/`) | ||
.reply(429, { | ||
sys: { | ||
type: `Error`, | ||
id: `RateLimitExceeded`, | ||
}, | ||
message: `You have exceeded the rate limit of the Organization this Space belongs to by making too many API requests within a short timespan. Please wait a moment before trying the request again.`, | ||
requestId: `123abc`, | ||
}) | ||
|
||
try { | ||
await fetchData({ | ||
pluginConfig: createPluginConfig({ | ||
...options, | ||
contentfulClientConfig: { retryOnError: false }, | ||
}), | ||
reporter, | ||
}) | ||
jest.fail() | ||
} catch (e) { | ||
expect(e.context.sourceMessage).toMatchSnapshot() | ||
} | ||
|
||
expect(reporter.panic).toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters