From b97a767af44aad50ab152c5c5d791989ff94ec31 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 25 Jan 2026 17:51:48 +0000 Subject: [PATCH 1/3] Initial plan From f01e18a0d9c45c2999175f4139401366103a0803 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 25 Jan 2026 17:55:59 +0000 Subject: [PATCH 2/3] Add HTTPError.body support to error handlers - Updated dev.ts and prod.ts to spread error.body properties at top level - Added test case for HTTPError.body in test fixture - Tests passing for both dev and prod error handlers Co-authored-by: pi0 <5158436+pi0@users.noreply.github.com> --- src/runtime/internal/error/dev.ts | 1 + src/runtime/internal/error/prod.ts | 1 + test/fixture/server/routes/api/error-body.ts | 12 ++++++++++++ test/tests.ts | 15 +++++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 test/fixture/server/routes/api/error-body.ts diff --git a/src/runtime/internal/error/dev.ts b/src/runtime/internal/error/dev.ts index 8ddc60abcc..77c8374576 100644 --- a/src/runtime/internal/error/dev.ts +++ b/src/runtime/internal/error/dev.ts @@ -86,6 +86,7 @@ export async function defaultHandler( statusText: error.statusText, message: error.message, data: error.data, + ...error.body, stack: error.stack?.split("\n").map((line) => line.trim()), } : await youch.toHTML(error, { diff --git a/src/runtime/internal/error/prod.ts b/src/runtime/internal/error/prod.ts index 750be30fff..649f8e1895 100644 --- a/src/runtime/internal/error/prod.ts +++ b/src/runtime/internal/error/prod.ts @@ -62,6 +62,7 @@ export function defaultHandler( statusText: error.statusText, message: isSensitive ? "Server Error" : error.message, data: isSensitive ? undefined : error.data, + ...(isSensitive ? {} : error.body), }; return { diff --git a/test/fixture/server/routes/api/error-body.ts b/test/fixture/server/routes/api/error-body.ts new file mode 100644 index 0000000000..fe215480f7 --- /dev/null +++ b/test/fixture/server/routes/api/error-body.ts @@ -0,0 +1,12 @@ +import { HTTPError } from "nitro/h3"; + +export default () => { + throw new HTTPError({ + status: 500, + message: "Custom error message", + data: { + instance: "local", + }, + body: { code: "urn:nitro:missing-error-code" }, + }); +}; diff --git a/test/tests.ts b/test/tests.ts index b608eb6ed3..a9280b76cf 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -422,6 +422,21 @@ export function testNitro( expect(data.json.error).toBe(true); }); + it("handles errors with body property", async () => { + const { status, data } = await callHandler({ + url: "/api/error-body", + headers: { + Accept: "application/json", + }, + }); + expect(status).toBe(500); + expect(data.error).toBe(true); + expect(data.message).toBe("Custom error message"); + expect(data.data).toEqual({ instance: "local" }); + // Body properties should be spread at top level + expect(data.code).toBe("urn:nitro:missing-error-code"); + }); + it.skipIf( // TODO! ctx.preset === "vercel" && ctx.nitro?.options.vercel?.entryFormat === "node" && isWindows From 64536985cf6461b2fc6731437ec3083a852e6430 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 25 Jan 2026 17:58:35 +0000 Subject: [PATCH 3/3] Add null/undefined safety to error.body spread - Added fallback to empty object for error.body to prevent runtime errors - Ensures safe spreading even when error.body is null or undefined Co-authored-by: pi0 <5158436+pi0@users.noreply.github.com> --- src/runtime/internal/error/dev.ts | 2 +- src/runtime/internal/error/prod.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/internal/error/dev.ts b/src/runtime/internal/error/dev.ts index 77c8374576..54eebeda5d 100644 --- a/src/runtime/internal/error/dev.ts +++ b/src/runtime/internal/error/dev.ts @@ -86,7 +86,7 @@ export async function defaultHandler( statusText: error.statusText, message: error.message, data: error.data, - ...error.body, + ...(error.body || {}), stack: error.stack?.split("\n").map((line) => line.trim()), } : await youch.toHTML(error, { diff --git a/src/runtime/internal/error/prod.ts b/src/runtime/internal/error/prod.ts index 649f8e1895..9af69471b7 100644 --- a/src/runtime/internal/error/prod.ts +++ b/src/runtime/internal/error/prod.ts @@ -62,7 +62,7 @@ export function defaultHandler( statusText: error.statusText, message: isSensitive ? "Server Error" : error.message, data: isSensitive ? undefined : error.data, - ...(isSensitive ? {} : error.body), + ...(isSensitive ? {} : error.body || {}), }; return {