Skip to content

Commit dc978ad

Browse files
committed
fix: Call onAfterResponse when onError returns a value (using aot)
1 parent dafd31b commit dc978ad

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
@@ -2049,6 +2049,7 @@ export const composeHandler = ({
20492049
errorReporter.resolve()
20502050
}
20512051

2052+
fnLiteral += afterResponse()
20522053
fnLiteral += `return er}`
20532054
}
20542055
}

test/lifecycle/response.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,41 @@ describe('On After Response Error', () => {
232232
expect(counter).toBe(1)
233233
}
234234
)
235+
236+
it.each([
237+
{ aot: true, onErrorReturnsValue: "error handled" },
238+
{ aot: false, onErrorReturnsValue: "error handled" },
239+
240+
{ aot: true, onErrorReturnsValue: { message: "error handled" } },
241+
{ aot: false, onErrorReturnsValue: { message: "error handled" } },
242+
])('should execute onAfterResponse when onError returns a value aot=$aot,\tonErrorReturnsValue=$onErrorReturnsValue', async ({ aot, onErrorReturnsValue }) => {
243+
let counter = 0
244+
245+
const app = new Elysia({ aot })
246+
.onError(() => {
247+
return onErrorReturnsValue
248+
})
249+
.onAfterResponse(() => {
250+
counter++
251+
})
252+
.get('/error', () => {
253+
throw new Error('test error')
254+
})
255+
256+
expect(counter).toBe(0)
257+
258+
const req = new Request('http://localhost/error')
259+
const res = await app.handle(req)
260+
const text = await res.text()
261+
262+
expect(text).toStrictEqual(
263+
typeof onErrorReturnsValue === 'string'
264+
? onErrorReturnsValue
265+
: JSON.stringify(onErrorReturnsValue)
266+
)
267+
268+
await Bun.sleep(1)
269+
270+
expect(counter).toBe(1)
271+
})
235272
})

0 commit comments

Comments
 (0)