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
1 change: 1 addition & 0 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,7 @@ export const composeHandler = ({
errorReporter.resolve()
}

fnLiteral += afterResponse()
fnLiteral += `return er}`
}
}
Expand Down
37 changes: 37 additions & 0 deletions test/lifecycle/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,41 @@ describe('On After Response Error', () => {
expect(counter).toBe(1)
}
)

it.each([
{ aot: true, onErrorReturnsValue: "error handled" },
{ aot: false, onErrorReturnsValue: "error handled" },

{ aot: true, onErrorReturnsValue: { message: "error handled" } },
{ aot: false, onErrorReturnsValue: { message: "error handled" } },
])('should execute onAfterResponse when onError returns a value aot=$aot,\tonErrorReturnsValue=$onErrorReturnsValue', async ({ aot, onErrorReturnsValue }) => {
let counter = 0

const app = new Elysia({ aot })
.onError(() => {
return onErrorReturnsValue
})
.onAfterResponse(() => {
counter++
})
.get('/error', () => {
throw new Error('test error')
})

expect(counter).toBe(0)

const req = new Request('http://localhost/error')
const res = await app.handle(req)
const text = await res.text()

expect(text).toStrictEqual(
typeof onErrorReturnsValue === 'string'
? onErrorReturnsValue
: JSON.stringify(onErrorReturnsValue)
)

await Bun.sleep(1)

expect(counter).toBe(1)
})
})