Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,8 @@ export const composeHandler = ({
total: hooks.mapResponse?.length
})

fnLiteral += `if(er!==undefined){`

for (let i = 0; i < hooks.mapResponse.length; i++) {
const mapResponse = hooks.mapResponse[i]

Expand All @@ -2097,6 +2099,7 @@ export const composeHandler = ({

endUnit()
}
fnLiteral += `}`
mapResponseReporter.resolve()
}

Expand Down
67 changes: 67 additions & 0 deletions test/lifecycle/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,73 @@ describe('error', () => {
expect(response.status).toBe(500)
})

it.each([true, false])(
'continue error handler chain when an earlier handler returns undefined with mapResponse and aot: %p',
async (aot) => {
const events: string[] = []

const logger = new Elysia().onError({ as: 'global' }, ({ error }) => {
events.push(
`logger:${error instanceof Error ? error.message : String(error)}`
)
})

const responseWrapper = new Elysia()
.onError({ as: 'global' }, ({ error, set }) => {
events.push('response-wrapper:onError')
set.status = 200

return Response.json({
code: 403,
msg:
error instanceof Error
? error.message
: 'Forbidden',
data: null
})
})
.mapResponse({ as: 'global' }, ({ response, set }) => {
events.push(
`response-wrapper:mapResponse:${response instanceof Response}`
)

if (response instanceof Response) return response

set.status = 200

return Response.json({
code: 200,
msg: 'success',
data: response ?? null
})
})

const guard = new Elysia().onBeforeHandle({ as: 'scoped' }, () => {
throw new Error('denied by nested guard')
})

const api = new Elysia({ prefix: '/api' }).group('', (app) =>
app.use(guard).post('/guarded', () => ({ ok: true }))
)

const app = new Elysia({ aot })
.use(logger)
.use(responseWrapper)
.use(api)

const response = await app.handle(post('/api/guarded', {}))

expect(response.status).toBe(200)
expect(await response.json()).toEqual({
code: 403,
msg: 'denied by nested guard',
data: null
})
expect(events).toContain('logger:denied by nested guard')
expect(events).toContain('response-wrapper:onError')
}
)

it.each([true, false])(
'return correct number status on error function with aot: %p',
async (aot) => {
Expand Down