diff --git a/src/compose.ts b/src/compose.ts index fa904055..cbc84e4c 100644 --- a/src/compose.ts +++ b/src/compose.ts @@ -1962,6 +1962,7 @@ export const composeHandler = ({ errorReporter.resolve() } + fnLiteral += afterResponse() fnLiteral += `return er}` } } diff --git a/test/lifecycle/response.test.ts b/test/lifecycle/response.test.ts index 3641a50f..ed202728 100644 --- a/test/lifecycle/response.test.ts +++ b/test/lifecycle/response.test.ts @@ -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) + }) })