fix(compose): fix it for error handling with `async` (#583)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 03c3dec · Browse files · 3 files·+53 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+53 added−12 removed
- tests:+23 added−0 removed
- other:+30 added−12 removed
Provenance
- Commit
4124512- Tree
7081357- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
03c3dec
3 files changed+53 added−12 removed
modified:deno_dist/compose.ts+15 added-6 removed
53 unmodified lines545556575858596061626359606162636465666768697071727374757653 unmodified lines context.res = res } return context } else { return res return res.then((res) => { if (res && context.finalized === false) { context.res = res } return context }) .then((res) => { if (res && context.finalized === false) { context.res = res } return context }) .catch((err) => { if (err instanceof Error && context instanceof HonoContext && onError) { context.error = err context.res = onError(err, context) return context } throw err }) } } }}modified:src/compose.ts+15 added-6 removed
53 unmodified lines545556575858596061626359606162636465666768697071727374757653 unmodified lines context.res = res } return context } else { return res return res.then((res) => { if (res && context.finalized === false) { context.res = res } return context }) .then((res) => { if (res && context.finalized === false) { context.res = res } return context }) .catch((err) => { if (err instanceof Error && context instanceof HonoContext && onError) { context.error = err context.res = onError(err, context) return context } throw err }) } } }}modified:src/hono.test.ts+23 added
568 unmodified lines569 c.res = c.text(`Handle the error in middleware, original message is ${message}`, 500)570 }571 })572 573+ app.get('/handle-error-in-middleware-async', async (c, next) => {574+ await next()575+ if (c.error) {576+ const message = c.error.message577+ c.res = c.text(578+ `Handle the error in middleware with async, original message is ${message}`,579+ 500580+ )581+ }582+ })583+584 app.get('/handle-error-in-middleware', () => {585 throw new Error('Error message')586 })587 588+ app.get('/handle-error-in-middleware-async', async () => {589+ throw new Error('Error message')590+ })591+592 it('Should handle the error in middleware', async () => {593 const res = await app.request('https://example.com/handle-error-in-middleware')594 expect(res.status).toBe(500)595 expect(await res.text()).toBe(596 'Handle the error in middleware, original message is Error message'597 )598 })599+600+ it('Should handle the error in middleware - async', async () => {601+ const res = await app.request('https://example.com/handle-error-in-middleware-async')602+ expect(res.status).toBe(500)603+ expect(await res.text()).toBe(604+ 'Handle the error in middleware with async, original message is Error message'605+ )606+ })607 })608 609 describe('Request methods with custom middleware', () => {610 const app = new Hono()529 unmodified lines