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
Provenance
- Commit
edfb31e- Tree
92e6c79- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
888e04d
4 files changed+32 added−12 removed
modified:deno_dist/utils/encode.ts+1 added-1 removed
11234572 unmodified linesimport { Buffer } from "https://deno.land/std@0.161.0/node/buffer.ts";import { Buffer } from "https://deno.land/std@0.162.0/node/buffer.ts";export const encodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "encodeBase64" should not be null.') }72 unmodified linesmodified:deno_dist/hono.ts+3 added-3 removed
200 unmodified lines201 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 linesmodified:src/hono.test.ts+25 added-5 removed
176 unmodified lines177 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- 181+182 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })183 expect(status).toBe(404)184 })185 186 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- 190+191 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })192 expect(status).toBe(404)193 })194 195 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- 199+200 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })201 expect(status).toBe(404)202 })203 204 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- 208+209 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })210 expect(status).toBe(404)211 })212 213 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- 217+218 const { status } = await one.request('http://localhost/two/three/hi', { method: 'GET' })219 expect(status).toBe(404)220 })221 })447 unmodified lines669 expect(await res.text()).toBe(670 'Handle the error in middleware with async, original message is Error message'671 )672 })673+674+ describe('Error in `notFound()`', () => {675+ const app = new Hono()676+677+ app.use('*', async () => {})678+679+ app.notFound(() => {680+ throw new Error('Error in Not Found')681+ })682+683+ app.onError((err, c) => {684+ return c.text(err.message, 400)685+ })686+687+ 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 })694 695 describe('Request methods with custom middleware', () => {696 const app = new Hono()591 unmodified linesmodified:src/hono.ts+3 added-3 removed
200 unmodified lines201 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