Skip to content

Commit b081bd5

Browse files
committed
fix: Call onAfterResponse when onError returns a value (using aot)
1 parent 668c7a1 commit b081bd5

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/compose.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,7 @@ export const composeHandler = ({
19621962
errorReporter.resolve()
19631963
}
19641964

1965+
fnLiteral += afterResponse()
19651966
fnLiteral += `return er}`
19661967
}
19671968
}

test/lifecycle/response.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,41 @@ describe('On After Response Error', () => {
267267
expect(counter).toBe(1)
268268
}
269269
)
270+
271+
it.each([
272+
{ aot: true, onErrorReturnsValue: "error handled" },
273+
{ aot: false, onErrorReturnsValue: "error handled" },
274+
275+
{ aot: true, onErrorReturnsValue: { message: "error handled" } },
276+
{ aot: false, onErrorReturnsValue: { message: "error handled" } },
277+
])('should execute onAfterResponse when onError returns a value aot=$aot,\tonErrorReturnsValue=$onErrorReturnsValue', async ({ aot, onErrorReturnsValue }) => {
278+
let counter = 0
279+
280+
const app = new Elysia({ aot })
281+
.onError(() => {
282+
return onErrorReturnsValue
283+
})
284+
.onAfterResponse(() => {
285+
counter++
286+
})
287+
.get('/error', () => {
288+
throw new Error('test error')
289+
})
290+
291+
expect(counter).toBe(0)
292+
293+
const req = new Request('http://localhost/error')
294+
const res = await app.handle(req)
295+
const text = await res.text()
296+
297+
expect(text).toStrictEqual(
298+
typeof onErrorReturnsValue === 'string'
299+
? onErrorReturnsValue
300+
: JSON.stringify(onErrorReturnsValue)
301+
)
302+
303+
await Bun.sleep(1)
304+
305+
expect(counter).toBe(1)
306+
})
270307
})

0 commit comments

Comments
 (0)