Skip to content
Myr

Revert "feat: use added app's errorHandler (#871)" (#872)

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

This reverts commit b8a99b53672520b0b23547906cfc4042931438d7.

authored by Yusuke Wada · committed by GitHub · parent b8a99b5 · Browse files · 6 files+30 added−141 removed
This commit

State

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

Diff stats

6 files+30 added−141 removed

  • tests:+4 added−101 removed
  • other:+26 added−40 removed

6 files+30 added−141 removed

modified:deno_dist/compose.ts+2 added-2 removed
modified:deno_dist/hono.ts+11 added-18 removed
59 unmodified lines
60 method: string61 handler: Handler<P, E, S>62}6364const notFoundHandler = <E extends Partial<Environment>>(c: Context<string, E>) => {65 return c.text('404 Not Found', 404)66}6768const errorHandler = <E extends Partial<Environment>>(err: Error, c: Context<string, E>) => {69 console.trace(err)70 const message = 'Internal Server Error'71 return c.text(message, 500)72}7364export class Hono<65 E extends Partial<Environment> = Environment,66 P extends string = string,67 S = unknown
34 unmodified lines
102103 Object.assign(this, init)104 }105116 private notFoundHandler: NotFoundHandler<E> = notFoundHandler117 private errorHandler: ErrorHandler<E> = errorHandler106 private notFoundHandler: NotFoundHandler<E> = (c: Context<string, E>) => {107 return c.text('404 Not Found', 404)108 }109110 private errorHandler: ErrorHandler<E> = (err: Error, c: Context<string, E>) => {111 console.trace(err)112 const message = 'Internal Server Error'113 return c.text(message, 500)114 }115116 // eslint-disable-next-line @typescript-eslint/no-explicit-any117 route(path: string, app?: Hono<any>) {118 this._tempPath = path119 if (app) {120 app.routes.map((r) => {124 const handler =125 app.errorHandler === errorHandler126 ? r.handler127 : compose<Context<P, E>, E>([r.handler], app.errorHandler)128 this.addRoute(r.method, r.path, handler as unknown as Handler<P, E, S>)121 this.addRoute(r.method, r.path, r.handler as unknown as Handler<P, E, S>)122 })123 this._tempPath = ''124 }125 return this
109 unmodified lines
235 })()236 }237238 const handlers = result ? result.handlers : [this.notFoundHandler]246 const composed = compose<Context<P, E>, E>(handlers, this.errorHandler, this.notFoundHandler)239 const composed = compose<Context<P, E>, E>(handlers, this.notFoundHandler, this.errorHandler)240241 return (async () => {242 try {243 // eslint-disable-next-line @typescript-eslint/no-explicit-any
27 unmodified lines
modified:src/compose.test.ts+4 added-4 removed
153 unmodified lines
154155 middleware.push(mHandler)156157 it('Should return 404 Response', async () => {158 const composed = compose<Context>(middleware, undefined, onNotFound)158 const composed = compose<Context>(middleware, onNotFound)159 const context = await composed(c)160 expect(context.res).not.toBeNull()161 expect(context.res.status).toBe(404)162 expect(await context.res.text()).toBe('onNotFound')
74 unmodified lines
237238 const onNotFound = (c: Context) => c.text('NotFound', 404)239 const onError = (_error: Error, c: Context) => c.text('onError', 500)240241 const composed = compose<Context>(middleware, onError, onNotFound)241 const composed = compose<Context>(middleware, onNotFound, onError)242 const context = await composed(c)243 expect(context.res).not.toBeNull()244 expect(context.res.status).toBe(500)245 expect(await context.res.text()).toBe('onError')
37 unmodified lines
283 const mHandler2 = async () => {}284285 middleware.push(mHandler)286 middleware.push(mHandler2)287 const composed = compose<Context>(middleware, undefined, onNotFound)287 const composed = compose<Context>(middleware, onNotFound)288 const context = await composed(c)289 expect(context.finalized).toBe(false)290 })291
5 unmodified lines
297 const handler = async () => {}298 middleware2.push(mHandler3)299 middleware2.push(handler)300301 const composed = compose<Context>(middleware2, undefined, onNotFound)301 const composed = compose<Context>(middleware2, onNotFound)302 const context = await composed(c)303 expect(context.finalized).toBe(false)304 })305})
357 unmodified lines
modified:src/compose.ts+2 added-2 removed
modified:src/hono.test.ts-97 removed
933 unmodified lines
934 expect(res.status).toBe(200)935 expect(await res.text()).toBe('book 123')936 })937 })938939 describe('onError', () => {940 const app = new Hono()941 const sub = new Hono()942943 app.use('*', async (c, next) => {944 await next()945 if (c.req.query('app-error')) {946 throw new Error('This is Error')947 }948 })949950 app.onError((err, c) => {951 return c.text('onError by app', 500)952 })953954 sub.get('/ok', (c) => {955 return c.text('ok')956 })957958 sub.get('/error', () => {959 throw new Error('This is Error')960 })961962 sub.onError((err, c) => {963 return c.text('onError by sub', 500)964 })965966 app.route('/sub', sub)967968 it('should be handled by app', async () => {969 const res = await app.request('https://example.com/sub/ok?app-error=1')970 expect(res.status).toBe(500)971 expect(await res.text()).toBe('onError by app')972 })973974 it('should be handled by sub', async () => {975 const res = await app.request('https://example.com/sub/error')976 expect(res.status).toBe(500)977 expect(await res.text()).toBe('onError by sub')978 })979 })980981 describe('notFound', () => {982 const app = new Hono()983 const sub = new Hono()984985 app.get('/explicit-404', async (c) => {986 c.header('explicit', '1')987 })988989 app.notFound((c) => {990 return c.text('404 Not Found by app', 404)991 })992993 sub.get('/ok', (c) => {994 return c.text('ok')995 })996997 sub.get('/explicit-404', async (c) => {998 c.header('explicit', '1')999 })10001001 sub.notFound((c) => {1002 return c.text('404 Not Found by sub', 404)1003 })10041005 app.route('/sub', sub)10061007 it('/explicit-404 should be handled on app', async () => {1008 const res = await app.request('https://example.com/explicit-404')1009 expect(res.status).toBe(404)1010 expect(res.headers.get('explicit')).toBe('1')1011 expect(await res.text()).toBe('404 Not Found by app')1012 })10131014 it('/sub/explicit-404 should be handled on app', async () => {1015 const res = await app.request('https://example.com/sub/explicit-404')1016 expect(res.status).toBe(404)1017 expect(res.headers.get('explicit')).toBe('1')1018 expect(await res.text()).toBe('404 Not Found by app')1019 })10201021 it('/implicit-404 should be handled by app', async () => {1022 const res = await app.request('https://example.com/implicit-404')1023 expect(res.status).toBe(404)1024 expect(res.headers.get('explicit')).toBe(null)1025 expect(await res.text()).toBe('404 Not Found by app')1026 })10271028 it('/sub/implicit-404 should be handled by sub', async () => {1029 const res = await app.request('https://example.com/sub/implicit-404')1030 expect(res.status).toBe(404)1031 expect(res.headers.get('explicit')).toBe(null)1032 expect(await res.text()).toBe('404 Not Found by app')1033 })1034 })938})939940describe('Using other methods with `app.on`', () => {941 it('Should handle PURGE method with StaticRouter', async () => {
504 unmodified lines
modified:src/hono.ts+11 added-18 removed
59 unmodified lines
60 method: string61 handler: Handler<P, E, S>62}6364const notFoundHandler = <E extends Partial<Environment>>(c: Context<string, E>) => {65 return c.text('404 Not Found', 404)66}6768const errorHandler = <E extends Partial<Environment>>(err: Error, c: Context<string, E>) => {69 console.trace(err)70 const message = 'Internal Server Error'71 return c.text(message, 500)72}7364export class Hono<65 E extends Partial<Environment> = Environment,66 P extends string = string,67 S = unknown
34 unmodified lines
102103 Object.assign(this, init)104 }105116 private notFoundHandler: NotFoundHandler<E> = notFoundHandler117 private errorHandler: ErrorHandler<E> = errorHandler106 private notFoundHandler: NotFoundHandler<E> = (c: Context<string, E>) => {107 return c.text('404 Not Found', 404)108 }109110 private errorHandler: ErrorHandler<E> = (err: Error, c: Context<string, E>) => {111 console.trace(err)112 const message = 'Internal Server Error'113 return c.text(message, 500)114 }115116 // eslint-disable-next-line @typescript-eslint/no-explicit-any117 route(path: string, app?: Hono<any>) {118 this._tempPath = path119 if (app) {120 app.routes.map((r) => {124 const handler =125 app.errorHandler === errorHandler126 ? r.handler127 : compose<Context<P, E>, E>([r.handler], app.errorHandler)128 this.addRoute(r.method, r.path, handler as unknown as Handler<P, E, S>)121 this.addRoute(r.method, r.path, r.handler as unknown as Handler<P, E, S>)122 })123 this._tempPath = ''124 }125 return this
109 unmodified lines
235 })()236 }237238 const handlers = result ? result.handlers : [this.notFoundHandler]246 const composed = compose<Context<P, E>, E>(handlers, this.errorHandler, this.notFoundHandler)239 const composed = compose<Context<P, E>, E>(handlers, this.notFoundHandler, this.errorHandler)240241 return (async () => {242 try {243 // eslint-disable-next-line @typescript-eslint/no-explicit-any
27 unmodified lines