Skip to content
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(core): Handle fetch rejections and pass to makeErrorResult #3131

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-kangaroos-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Handle `fetch` rejections in `makeFetchSource` and properly hand them over to `CombinedError`s.
13 changes: 13 additions & 0 deletions packages/core/src/internal/fetchSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ describe('on error', () => {
});
});

it('handles network errors', async () => {
const error = new Error('test');
fetch.mockRejectedValue(error);

const fetchOptions = {};
const data = await pipe(
makeFetchSource(queryOperation, 'https://test.com/graphql', fetchOptions),
toPromise
);

expect(data).toHaveProperty('error.networkError', error);
});

it('returns error data', async () => {
const fetchOptions = {};
const data = await pipe(
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/internal/fetchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async function* fetchOperation(
let networkMode = true;
let abortController: AbortController | void;
let result: OperationResult | null = null;
let response: Response;
let response: Response | void;

try {
if (typeof AbortController !== 'undefined') {
Expand Down Expand Up @@ -151,11 +151,12 @@ async function* fetchOperation(

yield makeErrorResult(
operation,
(response!.status < 200 || response!.status >= 300) &&
response!.statusText
? new Error(response!.statusText)
response &&
(response.status < 200 || response.status >= 300) &&
response.statusText
? new Error(response.statusText)
: error,
response!
response
);
} finally {
if (abortController) abortController.abort();
Expand Down