Skip to content
Myr

fix(error): make `notFound()` enables to catch errors correctly (#652)

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 888e04d · Browse files · 4 files+32 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

4 files+32 added−12 removed

  • tests:+25 added−5 removed
  • other:+7 added−7 removed

4 files+32 added−12 removed

modified:deno_dist/utils/encode.ts+1 added-1 removed
modified:deno_dist/hono.ts+3 added-3 removed
200 unmodified lines
201 return (async () => {202 let awaited: Response | undefined | void203 try {204 awaited = await res205 if (!awaited) {206 return this.notFoundHandler(c)207 }208 } catch (err) {209 return this.handleError(err, c)210 }208 if (!awaited) {209 return this.notFoundHandler(c)210 }211 return awaited212 })()213 }214
32 unmodified lines
modified:src/hono.test.ts+25 added-5 removed
176 unmodified lines
177 it('fails with incorrect order 1', async () => {178 three.get('/hi', (c) => c.text('hi'))179 one.route('/two', two)180 two.route('/three', three)181 181182 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })183 expect(status).toBe(404)184 })185186 it('fails with incorrect order 2', async () => {187 two.route('/three', three)188 three.get('/hi', (c) => c.text('hi'))189 one.route('/two', two)190 190191 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })192 expect(status).toBe(404)193 })194195 it('fails with incorrect order 3', async () => {196 two.route('/three', three)197 one.route('/two', two)198 three.get('/hi', (c) => c.text('hi'))199 199200 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })201 expect(status).toBe(404)202 })203204 it('fails with incorrect order 4', async () => {205 one.route('/two', two)206 three.get('/hi', (c) => c.text('hi'))207 two.route('/three', three)208 208209 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })210 expect(status).toBe(404)211 })212213 it('fails with incorrect order 5', async () => {214 one.route('/two', two)215 two.route('/three', three)216 three.get('/hi', (c) => c.text('hi'))217 217218 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })219 expect(status).toBe(404)220 })221 })
447 unmodified lines
669 expect(await res.text()).toBe(670 'Handle the error in middleware with async, original message is Error message'671 )672 })673674 describe('Error in `notFound()`', () => {675 const app = new Hono()676677 app.use('*', async () => {})678679 app.notFound(() => {680 throw new Error('Error in Not Found')681 })682683 app.onError((err, c) => {684 return c.text(err.message, 400)685 })686687 it('Should handle the error thrown in `notFound()``', async () => {688 const res = await app.request('http://localhost/')689 expect(res.status).toBe(400)690 expect(await res.text()).toBe('Error in Not Found')691 })692 })693})694695describe('Request methods with custom middleware', () => {696 const app = new Hono()
591 unmodified lines
modified:src/hono.ts+3 added-3 removed
200 unmodified lines
201 return (async () => {202 let awaited: Response | undefined | void203 try {204 awaited = await res205 if (!awaited) {206 return this.notFoundHandler(c)207 }208 } catch (err) {209 return this.handleError(err, c)210 }208 if (!awaited) {209 return this.notFoundHandler(c)210 }211 return awaited212 })()213 }214
32 unmodified lines