Skip to content

Commit

Permalink
Update fetch error types
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jul 28, 2023
1 parent 616731f commit a92c404
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions packages/utils/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async function wrappedFetch(url: string | URL, init?: RequestInit): Promise<Resp
try {
return await fetch(url, init);
} catch (e) {
throw new FetchError(e, url);
throw new FetchError(url, e);
}
}

Expand All @@ -19,7 +19,7 @@ export function isFetchAbortError(e: unknown): e is FetchError {
return e instanceof FetchError && e.type === "aborted";
}

export type FetchErrorType = "system" | "input" | "aborted" | "other" | "unknown";
export type FetchErrorType = "failed" | "input" | "aborted" | "unknown";

export type FetchErrorCause = NativeFetchError["cause"];

Expand All @@ -28,10 +28,10 @@ export class FetchError extends Error {
code: string;
cause?: FetchErrorCause;

constructor(e: unknown, url: string | URL) {
if (isNativeSystemFetchError(e)) {
constructor(url: string | URL, e: unknown) {
if (isNativeFetchFailedError(e)) {
super(`Request to ${url.toString()} failed, reason: ${e.cause.message}`);
this.type = "system";
this.type = "failed";
this.code = e.cause.code;
this.cause = e.cause;
} else if (isNativeFetchInputError(e)) {
Expand All @@ -43,11 +43,6 @@ export class FetchError extends Error {
super(`Request to ${url.toString()} was aborted`);
this.type = "aborted";
this.code = "ERR_ABORTED";
} else if (isNativeFetchError(e)) {
super(`Request to ${url.toString()} failed, reason: ${e.cause.message}`);
this.type = "other";
this.code = e.cause.code;
this.cause = e.cause;
} else {
super((e as Error).message);
this.type = "unknown";
Expand All @@ -60,7 +55,7 @@ export class FetchError extends Error {
/**
* ```
* TypeError: fetch failed
* cause: Error: more detailed message
* cause: Error: detailed error message
* code: 'ERROR_CODE'
* ```
*/
Expand All @@ -79,22 +74,30 @@ type NativeFetchError = Error & {
* syscall: 'connect',
* address: '127.0.0.1',
* port: 9596
*
* ---------------------------
* TypeError: fetch failed
* cause: Error: getaddrinfo ENOTFOUND non-existent-domain
* errno: -3008,
* code: 'ENOTFOUND',
* syscall: 'getaddrinfo',
* hostname: 'non-existent-domain'
* ---------------------------
* TypeError: fetch failed
* cause: SocketError: other side closed
* code: 'UND_ERR_SOCKET',
* socket: {}
* ```
*/
type NativeFetchSystemError = NativeFetchError & {
type NativeFetchFailedError = NativeFetchError & {
message: "fetch failed";
cause: {
errno: string;
syscall: string;
errno?: string;
syscall?: string;
address?: string;
port?: string;
hostname?: string;
socket?: object;
[prop: string]: unknown;
};
};

Expand Down Expand Up @@ -129,8 +132,8 @@ function isNativeFetchError(e: unknown): e is NativeFetchError {
);
}

function isNativeSystemFetchError(e: unknown): e is NativeFetchSystemError {
return isNativeFetchError(e) && (e as NativeFetchSystemError).cause.syscall !== undefined;
function isNativeFetchFailedError(e: unknown): e is NativeFetchFailedError {
return isNativeFetchError(e) && (e as NativeFetchFailedError).message === "fetch failed";
}

function isNativeFetchInputError(e: unknown): e is NativeFetchInputError {
Expand Down

0 comments on commit a92c404

Please sign in to comment.