Skip to content

Commit

Permalink
Merge pull request #889 from crishoj/handle-custom-status-reponse-in-…
Browse files Browse the repository at this point in the history
…resolve-without-aot

🔧 fix: handle `error()` in resolve with `aot: false`
  • Loading branch information
SaltyAom authored Nov 14, 2024
2 parents 7d94a7a + 61471f5 commit 6811268
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/dynamic-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,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)
Expand Down
11 changes: 8 additions & 3 deletions test/lifecycle/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})

0 comments on commit 6811268

Please sign in to comment.