Skip to content

Commit

Permalink
refactor: pass all fetch context to the error
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 23, 2023
1 parent 15a28fb commit b70e6b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
41 changes: 21 additions & 20 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { FetchOptions, FetchRequest, FetchResponse } from "./fetch";
import type {
FetchContext,
FetchOptions,
FetchRequest,
FetchResponse,
} from "./fetch";

export class FetchError<T = any> extends Error {
name = "FetchError";
Expand All @@ -12,20 +17,16 @@ export class FetchError<T = any> extends Error {
statusMessage?: string;
}

export function createFetchError<T = any>(
request: FetchRequest,
options: FetchOptions,
error?: Error,
response?: FetchResponse<T>
): FetchError<T> {
const errorMessage = error?.message || error?.toString() || "";
export function createFetchError<T = any>(ctx: FetchContext<T>): FetchError<T> {
const errorMessage = ctx.error?.message || ctx.error?.toString() || "";

const method = (request as Request)?.method || options?.method || "GET";
const url = (request as Request)?.url || String(request) || "/";
const method =
(ctx.request as Request)?.method || ctx.options?.method || "GET";
const url = (ctx.request as Request)?.url || String(ctx.request) || "/";
const requestStr = `[${method}] ${JSON.stringify(url)}`;

const statusStr = response
? `${response.status} ${JSON.stringify(response.statusText)}`
const statusStr = ctx.response
? `${ctx.response.status} ${JSON.stringify(ctx.response.statusText)}`
: "<no response>";

const message = `${requestStr}: ${statusStr}${
Expand All @@ -36,42 +37,42 @@ export function createFetchError<T = any>(

Object.defineProperty(fetchError, "request", {
get() {
return request;
return ctx.request;
},
});
Object.defineProperty(fetchError, "options", {
get() {
return options;
return ctx.options;
},
});
Object.defineProperty(fetchError, "response", {
get() {
return response;
return ctx.response;
},
});
Object.defineProperty(fetchError, "data", {
get() {
return response && response._data;
return ctx.response && ctx.response._data;
},
});
Object.defineProperty(fetchError, "status", {
get() {
return response && response.status;
return ctx.response && ctx.response.status;
},
});
Object.defineProperty(fetchError, "statusText", {
get() {
return response && response.statusText;
return ctx.response && ctx.response.statusText;
},
});
Object.defineProperty(fetchError, "statusCode", {
get() {
return response && response.status;
return ctx.response && ctx.response.status;
},
});
Object.defineProperty(fetchError, "statusMessage", {
get() {
return response && response.statusText;
return ctx.response && ctx.response.statusText;
},
});

Expand Down
7 changes: 1 addition & 6 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,7 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
}

// Throw normalized error
const error = createFetchError(
context.request,
context.options,
context.error,
context.response
);
const error = createFetchError(context);

// Only available on V8 based runtimes (https://v8.dev/docs/stack-trace-api)
if (Error.captureStackTrace) {
Expand Down

0 comments on commit b70e6b0

Please sign in to comment.