Skip to content
Myr

fix(compose): don't throw Error; set error response into `c.res` (#576)

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 61e24b0 · Browse files · 8 files+65 added−29 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+65 added−29 removed

  • tests:+13 added−13 removed
  • other:+52 added−16 removed

8 files+65 added−29 removed

modified:deno_dist/compose.ts+19 added-7 removed
modified:deno_dist/context.ts+2 added
21 unmodified lines
22 env: E['Bindings']23 event: FetchEvent24 executionCtx: ExecutionContext25 finalized: boolean26 error: Error | undefined2728 get res(): Response29 set res(_res: Response)30 header: (name: string, value: string, options?: { append?: boolean }) => void
27 unmodified lines
58{59 req: Request<RequestParamKeyType, D>60 env: E['Bindings']61 finalized: boolean62 error: Error | undefined = undefined6364 _status: StatusCode = 20065 private _executionCtx: FetchEvent | ExecutionContext | undefined66 private _pretty: boolean = false
172 unmodified lines
modified:deno_dist/hono.ts+5 added-1 removed
247 unmodified lines
248 })()249 }250251 const handlers = result ? result.handlers : [this.notFoundHandler]252 const composed = compose<HonoContext<string, E>, E>(handlers, this.notFoundHandler)252 const composed = compose<HonoContext<string, E>, E>(253 handlers,254 this.notFoundHandler,255 this.errorHandler256 )257258 return (async () => {259 try {260 const tmp = composed(c)
26 unmodified lines
modified:src/compose.test.ts+5 added-6 removed
228 unmodified lines
229 throw new Error()230 }231232 const mHandler = async (_c: Context, next: Function) => {233 try {234 await next()233 await next()235 } catch {236 return c.text('onError', 500)237 }234 }235236 middleware.push(mHandler)237 middleware.push(handler)238239 const onNotFound = (c: Context) => c.text('NotFound', 404)240 const onError = (_error: Error, c: Context) => c.text('onError', 500)241243 const composed = compose<Context>(middleware)242 const composed = compose<Context>(middleware, onNotFound, onError)243 const context = await composed(c)244 expect(context.res).not.toBeNull()245 expect(context.res.status).toBe(500)246 expect(await context.res.text()).toBe('onError')
420 unmodified lines
modified:src/compose.ts+19 added-7 removed
modified:src/context.ts+2 added
21 unmodified lines
22 env: E['Bindings']23 event: FetchEvent24 executionCtx: ExecutionContext25 finalized: boolean26 error: Error | undefined2728 get res(): Response29 set res(_res: Response)30 header: (name: string, value: string, options?: { append?: boolean }) => void
27 unmodified lines
58{59 req: Request<RequestParamKeyType, D>60 env: E['Bindings']61 finalized: boolean62 error: Error | undefined = undefined6364 _status: StatusCode = 20065 private _executionCtx: FetchEvent | ExecutionContext | undefined66 private _pretty: boolean = false
172 unmodified lines
modified:src/hono.test.ts+8 added-7 removed
561 unmodified lines
562describe('Error handling in middleware', () => {563 const app = new Hono()564565 app.get('/handle-error-in-middleware', async (c, next) => {566 try {567 await next()568 } catch {569 c.res = c.text('Handle the error in middleware', 500)566 await next()567 if (c.error) {568 const message = c.error.message569 c.res = c.text(`Handle the error in middleware, original message is ${message}`, 500)570 }571 })572573 app.get('/handle-error-in-middleware', () => {574 console.log('throw new Error')575 throw new Error('This is Error')574 throw new Error('Error message')575 })576577 it('Should handle the error in middleware', async () => {578 const res = await app.request('https://example.com/handle-error-in-middleware')579 expect(res.status).toBe(500)581 expect(await res.text()).toBe('Handle the error in middleware')580 expect(await res.text()).toBe(581 'Handle the error in middleware, original message is Error message'582 )583 })584})585586describe('Request methods with custom middleware', () => {
530 unmodified lines
modified:src/hono.ts+5 added-1 removed
247 unmodified lines
248 })()249 }250251 const handlers = result ? result.handlers : [this.notFoundHandler]252 const composed = compose<HonoContext<string, E>, E>(handlers, this.notFoundHandler)252 const composed = compose<HonoContext<string, E>, E>(253 handlers,254 this.notFoundHandler,255 this.errorHandler256 )257258 return (async () => {259 try {260 const tmp = composed(c)
26 unmodified lines