Skip to content
Myr

fix(hono-base): fix not found handler calling timing when one middleware (#2080)

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

authored by Taku Amano · committed by GitHub · parent 27521e6 · Browse files · 3 files+76 added−32 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+76 added−32 removed

  • tests:+58 added−0 removed
  • other:+18 added−32 removed

3 files+76 added−32 removed

modified:deno_dist/hono-base.ts+9 added-16 removed
306 unmodified lines
307 // Do not `compose` if it has only one handler308 if (matchResult[0].length === 1) {309 let res: ReturnType<H>310 try {311 res = matchResult[0][0][0][0](c, async () => {})312 if (!res) {313 return this.notFoundHandler(c)314 }311 res = matchResult[0][0][0][0](c, async () => {312 c.res = await this.notFoundHandler(c)313 })314 } catch (err) {315 return this.handleError(err, c)316 }317318 if (res instanceof Response) return res319321 return (async () => {322 let awaited: Response | void323 try {324 awaited = await res325 if (!awaited) {326 return this.notFoundHandler(c)327 }328 } catch (err) {329 return this.handleError(err, c)330 }331 return awaited332 })()320 return res321 .then(322 (resolved: Response | undefined) =>323 resolved || (c.finalized ? c.res : this.notFoundHandler(c))324 )325 .catch((err: Error) => this.handleError(err, c))326 }327328 const composed = compose<Context>(matchResult[0], this.errorHandler, this.notFoundHandler)329
56 unmodified lines
modified:src/hono-base.ts+9 added-16 removed
306 unmodified lines
307 // Do not `compose` if it has only one handler308 if (matchResult[0].length === 1) {309 let res: ReturnType<H>310 try {311 res = matchResult[0][0][0][0](c, async () => {})312 if (!res) {313 return this.notFoundHandler(c)314 }311 res = matchResult[0][0][0][0](c, async () => {312 c.res = await this.notFoundHandler(c)313 })314 } catch (err) {315 return this.handleError(err, c)316 }317318 if (res instanceof Response) return res319321 return (async () => {322 let awaited: Response | void323 try {324 awaited = await res325 if (!awaited) {326 return this.notFoundHandler(c)327 }328 } catch (err) {329 return this.handleError(err, c)330 }331 return awaited332 })()320 return res321 .then(322 (resolved: Response | undefined) =>323 resolved || (c.finalized ? c.res : this.notFoundHandler(c))324 )325 .catch((err: Error) => this.handleError(err, c))326 }327328 const composed = compose<Context>(matchResult[0], this.errorHandler, this.notFoundHandler)329
56 unmodified lines
modified:src/hono.test.ts+58 added
1049 unmodified lines
1050 res = await app.request('http://localhost/foo')1051 expect(res.status).toBe(404)1052 expect(await res.text()).toBe('Custom 404 Not Found')1053 })10541055 describe('Not Found with a middleware', () => {1056 const app = new Hono()10571058 app.get('/', (c) => c.text('hello'))1059 app.use('*', async (c, next) => {1060 await next()1061 c.res = new Response((await c.res.text()) + ' + Middleware', c.res)1062 })10631064 it('Custom 404 Not Found', async () => {1065 let res = await app.request('http://localhost/')1066 expect(res.status).toBe(200)1067 expect(await res.text()).toBe('hello')1068 res = await app.request('http://localhost/foo')1069 expect(res.status).toBe(404)1070 expect(await res.text()).toBe('404 Not Found + Middleware')1071 })1072 })10731074 describe('Not Found with some middleware', () => {1075 const app = new Hono()10761077 app.get('/', (c) => c.text('hello'))1078 app.use('*', async (c, next) => {1079 await next()1080 c.res = new Response((await c.res.text()) + ' + Middleware 1', c.res)1081 })1082 app.use('*', async (c, next) => {1083 await next()1084 c.res = new Response((await c.res.text()) + ' + Middleware 2', c.res)1085 })10861087 it('Custom 404 Not Found', async () => {1088 let res = await app.request('http://localhost/')1089 expect(res.status).toBe(200)1090 expect(await res.text()).toBe('hello')1091 res = await app.request('http://localhost/foo')1092 expect(res.status).toBe(404)1093 expect(await res.text()).toBe('404 Not Found + Middleware 2 + Middleware 1')1094 })1095 })10961097 describe('No response from a handler', () => {1098 const app = new Hono()10991100 app.get('/', (c) => c.text('hello'))1101 app.get('/not-found', async (c) => undefined)11021103 it('Custom 404 Not Found', async () => {1104 let res = await app.request('http://localhost/')1105 expect(res.status).toBe(200)1106 expect(await res.text()).toBe('hello')1107 res = await app.request('http://localhost/not-found')1108 expect(res.status).toBe(404)1109 expect(await res.text()).toBe('404 Not Found')1110 })1111 })1112})11131114describe('Redirect', () => {1115 const app = new Hono()
2186 unmodified lines