Skip to content

Commit

Permalink
Handle aggregate fetch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jul 27, 2023
1 parent 461e063 commit cd00212
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/utils/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type NativeFetchAbortError = DOMException & {
name: "AbortError";
};

type NativeAggregateFetchError = Error & {
cause: AggregateError;
};

function isNativeFetchError(e: unknown): e is NativeFetchError {
return (
e instanceof Error &&
Expand All @@ -22,6 +26,10 @@ function isNativeFetchAbortError(e: unknown): e is NativeFetchAbortError {
return e instanceof DOMException && e.name === "AbortError";
}

function isNativeAggregateFetchError(e: unknown): e is NativeAggregateFetchError {
return e instanceof Error && (e as NativeAggregateFetchError).cause instanceof AggregateError;
}

class FetchBaseError extends Error {
constructor(message: string) {
super(message);
Expand Down Expand Up @@ -61,10 +69,10 @@ async function wrappedFetch(...args: Parameters<typeof fetch>): Promise<Response
try {
return await fetch(...args);
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
if (isNativeFetchError(e)) {
throw new FetchError(e);
} else if (isNativeAggregateFetchError(e)) {
throw new FetchError(e.cause.errors[0]);
} else if (isNativeFetchAbortError(e)) {
throw new FetchAbortError();
}
Expand Down

0 comments on commit cd00212

Please sign in to comment.