Skip to content
Merged
Changes from 1 commit
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
Next Next commit
fix(fetch): use try-catch block to capture fetching errors
  • Loading branch information
arunanshub committed Jun 5, 2023
commit c74a6e43e3ea3c7e331ff9a8b2537d282e12f6cb
16 changes: 9 additions & 7 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,18 @@ export function createFetch(globalOptions: CreateFetchOptions): $Fetch {
}
}

context.response = await fetch(
context.request,
context.options as RequestInit
).catch(async (error) => {
context.error = error;
try {
context.response = await fetch(
context.request,
context.options as RequestInit
);
} catch (error) {
context.error = error as Error;
if (context.options.onRequestError) {
await context.options.onRequestError(context as any);
}
return onError(context);
});
return await onError(context);
}

const responseType =
(context.options.parseResponse ? "json" : context.options.responseType) ||
Expand Down