From b70e6b0102ea226784304b5c144beaa17d734453 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 23 Aug 2023 19:32:42 +0200 Subject: [PATCH] refactor: pass all fetch context to the error --- src/error.ts | 41 +++++++++++++++++++++-------------------- src/fetch.ts | 7 +------ 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/error.ts b/src/error.ts index 8fbc950..34c5c78 100644 --- a/src/error.ts +++ b/src/error.ts @@ -1,4 +1,9 @@ -import type { FetchOptions, FetchRequest, FetchResponse } from "./fetch"; +import type { + FetchContext, + FetchOptions, + FetchRequest, + FetchResponse, +} from "./fetch"; export class FetchError extends Error { name = "FetchError"; @@ -12,20 +17,16 @@ export class FetchError extends Error { statusMessage?: string; } -export function createFetchError( - request: FetchRequest, - options: FetchOptions, - error?: Error, - response?: FetchResponse -): FetchError { - const errorMessage = error?.message || error?.toString() || ""; +export function createFetchError(ctx: FetchContext): FetchError { + 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)}` : ""; const message = `${requestStr}: ${statusStr}${ @@ -36,42 +37,42 @@ export function createFetchError( 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; }, }); diff --git a/src/fetch.ts b/src/fetch.ts index a4e8bf6..c7107b9 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -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) {