From 61471f51cb786bde610a953b1bae2714f828a77b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Rish=C3=B8j?= Date: Mon, 21 Oct 2024 23:23:09 +0200 Subject: [PATCH] :wrench: fix: handle `error()` in resolve with `aot: false` (fixes #888) --- src/dynamic-handle.ts | 4 ++++ test/lifecycle/resolve.test.ts | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/dynamic-handle.ts b/src/dynamic-handle.ts index 7ab208dc..158c1982 100644 --- a/src/dynamic-handle.ts +++ b/src/dynamic-handle.ts @@ -294,6 +294,10 @@ export const createDynamicHandler = let response = hook.fn(context) if (hook.subType === 'resolve') { + if (response instanceof ElysiaCustomStatusResponse) { + const result = mapEarlyResponse(response, context.set) + if (result) return (context.response = result) + } if (response instanceof Promise) Object.assign(context, await response) else Object.assign(context, response) diff --git a/test/lifecycle/resolve.test.ts b/test/lifecycle/resolve.test.ts index 68072762..a0d87c94 100644 --- a/test/lifecycle/resolve.test.ts +++ b/test/lifecycle/resolve.test.ts @@ -210,14 +210,19 @@ describe('resolve', () => { }) it('handle error', async () => { - const app = new Elysia() + const route = new Elysia() .resolve(() => { return error(418) }) .get('/', () => '') - const res = await app.handle(req('/')).then((x) => x.text()) + const res = await (new Elysia({aot: true})).use(route).handle(req('/')) + expect(await res.status).toEqual(418) + expect(await res.text()).toEqual("I'm a teapot") + + const res2 = await (new Elysia({aot: false})).use(route).handle(req('/')) + expect(await res2.status).toEqual(418) + expect(await res2.text()).toEqual("I'm a teapot") - expect(res).toEqual("I'm a teapot") }) })