Skip to content

Commit 6811268

Browse files
authored
Merge pull request #889 from crishoj/handle-custom-status-reponse-in-resolve-without-aot
🔧 fix: handle `error()` in resolve with `aot: false`
2 parents 7d94a7a + 61471f5 commit 6811268

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/dynamic-handle.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ export const createDynamicHandler =
295295
let response = hook.fn(context)
296296

297297
if (hook.subType === 'resolve') {
298+
if (response instanceof ElysiaCustomStatusResponse) {
299+
const result = mapEarlyResponse(response, context.set)
300+
if (result) return (context.response = result)
301+
}
298302
if (response instanceof Promise)
299303
Object.assign(context, await response)
300304
else Object.assign(context, response)

test/lifecycle/resolve.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,19 @@ describe('resolve', () => {
210210
})
211211

212212
it('handle error', async () => {
213-
const app = new Elysia()
213+
const route = new Elysia()
214214
.resolve(() => {
215215
return error(418)
216216
})
217217
.get('/', () => '')
218218

219-
const res = await app.handle(req('/')).then((x) => x.text())
219+
const res = await (new Elysia({aot: true})).use(route).handle(req('/'))
220+
expect(await res.status).toEqual(418)
221+
expect(await res.text()).toEqual("I'm a teapot")
222+
223+
const res2 = await (new Elysia({aot: false})).use(route).handle(req('/'))
224+
expect(await res2.status).toEqual(418)
225+
expect(await res2.text()).toEqual("I'm a teapot")
220226

221-
expect(res).toEqual("I'm a teapot")
222227
})
223228
})

0 commit comments

Comments
 (0)