-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
fix(gatsby-source-contentful): Improve network error handling #30257
Merged
wardpeet
merged 18 commits into
master
from
fix/contentful-improve-network-error-handling
Mar 29, 2021
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8b454e9
fix: do not log empty values when logging requests
axe312ger 3568845
fix: remove progress total as the real total is not available
axe312ger 227754c
fix: allow passing of any option to Contentful SDK again
axe312ger 8946b74
fix: pretty print Contentful errors after all retry attempts failed
axe312ger 1226330
Merge branch 'master' into fix/contentful-improve-network-error-handling
axe312ger 6da14b6
docs: add reintroduced contentfulClientConfig to the plugin validation
axe312ger 935f1d6
docs: add missing quote
axe312ger 9b03227
fix: allow error responses with JSON strings
axe312ger f2a6e7b
fix: do not pass error object to reporter.panic to display custom mes…
axe312ger 7f0b22a
style: clean up error message creation function
axe312ger 2d2dc07
Update packages/gatsby-source-contentful/src/gatsby-node.js
axe312ger e013695
test: revert change to unrelated snapshot
axe312ger 4a8ac8d
style: improve code style
axe312ger ab66282
style: simplify optional chaining
axe312ger 5a289ff
refactor(contentful): catch string only error responses and simplify
axe312ger 8dca766
style: optional chaning is required for images
axe312ger dab281c
Merge branch 'master' into fix/contentful-improve-network-error-handling
axe312ger d4716d5
revert normalize changes
wardpeet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
219 changes: 219 additions & 0 deletions
219
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,219 @@ | ||
/** | ||
* @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, | ||
contentfulClientConfig: { | ||
retryLimit: 2, | ||
}, | ||
} | ||
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(`fetch-retry`, () => { | ||
afterEach(() => { | ||
nock.cleanAll() | ||
reporter.verbose.mockClear() | ||
reporter.panic.mockClear() | ||
}) | ||
|
||
test(`request retries when network timeout happens`, 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.panic).not.toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
|
||
test(`request should fail after to many retries`, async () => { | ||
// Due to the retries, this can take up to 10 seconds | ||
jest.setTimeout(10000) | ||
|
||
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(3) | ||
.reply( | ||
500, | ||
{ | ||
sys: { | ||
type: `Error`, | ||
id: `MockedContentfulError`, | ||
}, | ||
message: `Mocked message of Contentful error`, | ||
}, | ||
{ [`x-contentful-request-id`]: `123abc` } | ||
) | ||
|
||
try { | ||
await fetchData({ pluginConfig, reporter }) | ||
jest.fail() | ||
} catch (e) { | ||
const msg = expect(e.context.sourceMessage) | ||
msg.toEqual( | ||
expect.stringContaining( | ||
`Fetching contentful data failed: 500 MockedContentfulError` | ||
) | ||
) | ||
msg.toEqual(expect.stringContaining(`Request ID: 123abc`)) | ||
msg.toEqual( | ||
expect.stringContaining(`The request was sent with 3 attempts`) | ||
) | ||
} | ||
expect(reporter.panic).toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe(`fetch-network-errors`, () => { | ||
test(`catches plain network error`, async () => { | ||
const scope = nock(baseURI) | ||
// Space | ||
.get(`/spaces/${options.spaceId}/`) | ||
.replyWithError({ code: `ECONNRESET` }) | ||
try { | ||
await fetchData({ | ||
pluginConfig: createPluginConfig({ | ||
...options, | ||
contentfulClientConfig: { retryOnError: false }, | ||
}), | ||
reporter, | ||
}) | ||
jest.fail() | ||
} catch (e) { | ||
expect(e.context.sourceMessage).toEqual( | ||
expect.stringContaining( | ||
`Accessing your Contentful space failed: ECONNRESET` | ||
) | ||
) | ||
} | ||
|
||
expect(reporter.panic).toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
|
||
test(`catches 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).toEqual( | ||
expect.stringContaining( | ||
`Accessing your Contentful space failed: Bad Gateway` | ||
) | ||
) | ||
} | ||
|
||
expect(reporter.panic).toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
|
||
test(`catches error with response object`, async () => { | ||
const scope = nock(baseURI) | ||
// Space | ||
.get(`/spaces/${options.spaceId}/`) | ||
.reply(429, { | ||
sys: { | ||
type: `Error`, | ||
id: `MockedContentfulError`, | ||
}, | ||
message: `Mocked message of Contentful error`, | ||
requestId: `123abc`, | ||
}) | ||
|
||
try { | ||
await fetchData({ | ||
pluginConfig: createPluginConfig({ | ||
...options, | ||
contentfulClientConfig: { retryOnError: false }, | ||
}), | ||
reporter, | ||
}) | ||
jest.fail() | ||
} catch (e) { | ||
const msg = expect(e.context.sourceMessage) | ||
|
||
msg.toEqual( | ||
expect.stringContaining( | ||
`Accessing your Contentful space failed: MockedContentfulError` | ||
) | ||
) | ||
msg.toEqual(expect.stringContaining(`Mocked message of Contentful error`)) | ||
msg.toEqual(expect.stringContaining(`Request ID: 123abc`)) | ||
} | ||
|
||
expect(reporter.panic).toBeCalled() | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add
retryLimit
in the SDK readme?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but if you do, pls make sure to add the other potential missing config options there as well :)