diff --git a/src/error.ts b/src/error.ts index f1625f60..40a9355e 100644 --- a/src/error.ts +++ b/src/error.ts @@ -24,7 +24,7 @@ export function createFetchError( const errorMessage = ctx.error?.message || ctx.error?.toString() || ""; const method = - (ctx.request as Request)?.method || ctx.options?.method || "GET"; + ctx.options?.method || (ctx.request as Request)?.method || "GET"; const url = (ctx.request as Request)?.url || String(ctx.request) || "/"; const requestStr = `[${method}] ${JSON.stringify(url)}`; diff --git a/test/index.test.ts b/test/index.test.ts index 5ac20b07..67a90db8 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -321,6 +321,18 @@ describe("ofetch", () => { expect(error.response?._data).to.deep.eq(error.data); }); + it("prefers options method over request method in error", async () => { + const error = await $fetch( + new Request(getURL("/403"), { method: "GET" }), + { method: "POST" } + ).catch((error: any) => error); + expect(error.toString()).toBe( + `FetchError: [POST] "${getURL("403")}": 403 Forbidden` + ); + expect(error.request).toBeInstanceOf(Request); + expect(error.options.method).to.equal("POST"); + }); + it("aborting on timeout", async () => { const noTimeout = $fetch(getURL("timeout")).catch(() => "no timeout"); const timeout = $fetch(getURL("timeout"), {