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
Provenance
- Commit
229228a- Tree
f70965b- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
b8a99b5
6 files changed+30 added−141 removed
modified:deno_dist/compose.ts+2 added-2 removed
7 unmodified lines891011121312131415161759 unmodified lines7 unmodified lines
// Based on the code in the MIT licensed `koa-compose` package.export const compose = <C extends ComposeContext, E extends Partial<Environment> = Environment>( middleware: Function[], onError?: ErrorHandler<E>, onNotFound?: NotFoundHandler<E> onNotFound?: NotFoundHandler<E>, onError?: ErrorHandler<E>) => { const middlewareLength = middleware.length return (context: C, next?: Function) => { let index = -159 unmodified linesmodified:deno_dist/hono.ts+11 added-18 removed
59 unmodified lines60 method: string61 handler: Handler<P, E, S>62 }63 64-const notFoundHandler = <E extends Partial<Environment>>(c: Context<string, E>) => {65- return c.text('404 Not Found', 404)66-}67-68-const 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-}73-64 export class Hono<65 E extends Partial<Environment> = Environment,66 P extends string = string,67 S = unknown34 unmodified lines102 103 Object.assign(this, init)104 }105 116- 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+ }109 110+ 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+ }115+116 // 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 this109 unmodified lines235 })()236 }237 238 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)240 241 return (async () => {242 try {243 // eslint-disable-next-line @typescript-eslint/no-explicit-any27 unmodified linesmodified:src/compose.test.ts+4 added-4 removed
153 unmodified lines154 155 middleware.push(mHandler)156 157 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 lines237 238 const onNotFound = (c: Context) => c.text('NotFound', 404)239 const onError = (_error: Error, c: Context) => c.text('onError', 500)240 241- 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 lines283 const mHandler2 = async () => {}284 285 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 lines297 const handler = async () => {}298 middleware2.push(mHandler3)299 middleware2.push(handler)300 301- 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 linesmodified:src/compose.ts+2 added-2 removed
7 unmodified lines891011121312131415161759 unmodified lines7 unmodified lines
// Based on the code in the MIT licensed `koa-compose` package.export const compose = <C extends ComposeContext, E extends Partial<Environment> = Environment>( middleware: Function[], onError?: ErrorHandler<E>, onNotFound?: NotFoundHandler<E> onNotFound?: NotFoundHandler<E>, onError?: ErrorHandler<E>) => { const middlewareLength = middleware.length return (context: C, next?: Function) => { let index = -159 unmodified linesmodified:src/hono.test.ts-97 removed
933 unmodified lines934 expect(res.status).toBe(200)935 expect(await res.text()).toBe('book 123')936 })937 })938-939- describe('onError', () => {940- const app = new Hono()941- const sub = new Hono()942-943- app.use('*', async (c, next) => {944- await next()945- if (c.req.query('app-error')) {946- throw new Error('This is Error')947- }948- })949-950- app.onError((err, c) => {951- return c.text('onError by app', 500)952- })953-954- sub.get('/ok', (c) => {955- return c.text('ok')956- })957-958- sub.get('/error', () => {959- throw new Error('This is Error')960- })961-962- sub.onError((err, c) => {963- return c.text('onError by sub', 500)964- })965-966- app.route('/sub', sub)967-968- 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- })973-974- 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- })980-981- describe('notFound', () => {982- const app = new Hono()983- const sub = new Hono()984-985- app.get('/explicit-404', async (c) => {986- c.header('explicit', '1')987- })988-989- app.notFound((c) => {990- return c.text('404 Not Found by app', 404)991- })992-993- sub.get('/ok', (c) => {994- return c.text('ok')995- })996-997- sub.get('/explicit-404', async (c) => {998- c.header('explicit', '1')999- })1000-1001- sub.notFound((c) => {1002- return c.text('404 Not Found by sub', 404)1003- })1004-1005- app.route('/sub', sub)1006-1007- 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- })1013-1014- 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- })1020-1021- 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- })1027-1028- 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 })939 940 describe('Using other methods with `app.on`', () => {941 it('Should handle PURGE method with StaticRouter', async () => {504 unmodified linesmodified:src/hono.ts+11 added-18 removed
59 unmodified lines60 method: string61 handler: Handler<P, E, S>62 }63 64-const notFoundHandler = <E extends Partial<Environment>>(c: Context<string, E>) => {65- return c.text('404 Not Found', 404)66-}67-68-const 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-}73-64 export class Hono<65 E extends Partial<Environment> = Environment,66 P extends string = string,67 S = unknown34 unmodified lines102 103 Object.assign(this, init)104 }105 116- 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+ }109 110+ 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+ }115+116 // 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 this109 unmodified lines235 })()236 }237 238 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)240 241 return (async () => {242 try {243 // eslint-disable-next-line @typescript-eslint/no-explicit-any27 unmodified lines