From 34624d65634990e307844e7e1f2a5c4281018015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=83=9C?= <2318857637@qq.com> Date: Sun, 14 Jun 2026 14:31:34 +0800 Subject: [PATCH] fix: prefer registered error code --- src/compose.ts | 6 +++--- src/dynamic-handle.ts | 10 +++++++++- test/lifecycle/error.test.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/compose.ts b/src/compose.ts index bc3b5dfe4..fbe2714f6 100644 --- a/src/compose.ts +++ b/src/compose.ts @@ -2058,8 +2058,8 @@ export const composeHandler = ({ 'c.code="VALIDATION"\n' + 'c.set.status=422' + '}else{' + - `c.code=error.code??error[ERROR_CODE]??"UNKNOWN"}` - else fnLiteral += `c.code=error.code??error[ERROR_CODE]??"UNKNOWN"\n` + `c.code=error[ERROR_CODE]??error.code??"UNKNOWN"}` + else fnLiteral += `c.code=error[ERROR_CODE]??error.code??"UNKNOWN"\n` fnLiteral += `let er\n` // Mapped error Response @@ -2657,7 +2657,7 @@ export const composeErrorHandler = (app: AnyElysia) => { fnLiteral += `const set=context.set\n` + `let _r\n` + - `if(!context.code)context.code=error.code??error[ERROR_CODE]\n` + + `if(!context.code)context.code=error[ERROR_CODE]??error.code\n` + `if(!(context.error instanceof Error))context.error=error\n` + `if(error instanceof ElysiaCustomStatusResponse){` + `set.status=error.status=error.code\n` + diff --git a/src/dynamic-handle.ts b/src/dynamic-handle.ts index c4dcd5198..d6e85b919 100644 --- a/src/dynamic-handle.ts +++ b/src/dynamic-handle.ts @@ -3,6 +3,7 @@ import type { Context } from './context' import { parseCookie } from './cookies' import { ElysiaCustomStatusResponse, + ERROR_CODE, type ElysiaErrors, NotFoundError, status, @@ -880,7 +881,14 @@ export const createDynamicErrorHandler = (app: AnyElysia) => { }, error: ElysiaErrors ) => { - const errorContext = Object.assign(context, { error, code: error.code }) + const registeredCode = (error as Error & { [ERROR_CODE]?: string })[ + ERROR_CODE + ] + + const errorContext = Object.assign(context, { + error, + code: registeredCode ?? error.code + }) errorContext.set = context.set if ( diff --git a/test/lifecycle/error.test.ts b/test/lifecycle/error.test.ts index 743708f4f..f196fdb32 100644 --- a/test/lifecycle/error.test.ts +++ b/test/lifecycle/error.test.ts @@ -124,6 +124,34 @@ describe('error', () => { expect(response.status).toBe(500) }) + it.each([true, false])( + 'uses registered error key over instance code with aot: %p', + async (aot) => { + class InvalidUrlError extends Error { + code = 'invalid_url' as const + + constructor() { + super('internal message') + } + } + + const app = new Elysia({ aot }) + .error({ INVALID_URL: InvalidUrlError }) + .onError(({ code }) => { + if (code === 'INVALID_URL') + return new Response('handled', { status: 422 }) + }) + .get('/', () => { + throw new InvalidUrlError() + }) + + const response = await app.handle(req('/')) + + expect(response.status).toBe(422) + expect(await response.text()).toBe('handled') + } + ) + it.each([true, false])( 'return correct number status on error function with aot: %p', async (aot) => {