Skip to content
Myr

feat(hono-base): skip import HTTPException by using HTTPResponseError (#2898)

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

This change is intended to make error handling more flexible and reduce bundle size. This idea is originated from https://github.com/honojs/hono/pull/2885

authored by Taku Amano · committed by GitHub · parent d260d21 · Browse files · 3 files+28 added−4 removed
This commit

State

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

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

3 files+28 added−4 removed

  • tests:+20 added−0 removed
  • other:+8 added−4 removed

3 files+28 added−4 removed

modified:src/hono-base.ts+3 added-3 removed
5 unmodified lines
6/* eslint-disable @typescript-eslint/no-explicit-any */7import { compose } from './compose'8import { Context } from './context'9import type { ExecutionContext } from './context'10import { HTTPException } from './http-exception'10import { HonoRequest } from './request'11import type { Router } from './router'12import { METHODS, METHOD_NAME_ALL, METHOD_NAME_ALL_LOWERCASE } from './router'13import type {14 Env,15 ErrorHandler,16 FetchEventLike,17 H,18 HTTPResponseError,19 HandlerInterface,20 MergePath,21 MergeSchemaPath,22 MiddlewareHandler,
14 unmodified lines
37const notFoundHandler = (c: Context) => {38 return c.text('404 Not Found', 404)39}4041const errorHandler = (err: Error, c: Context) => {42 if (err instanceof HTTPException) {41const errorHandler = (err: Error | HTTPResponseError, c: Context) => {42 if ('getResponse' in err) {43 return err.getResponse()44 }45 console.error(err)46 return c.text('Internal Server Error', 500)
484 unmodified lines
modified:src/hono.test.ts+20 added
1477 unmodified lines
1478 expect(res.status).toBe(401)1479 expect(await res.text()).toBe('Custom Error Message')1480 })1481 })14821483 describe('Handle HTTPException like object', () => {1484 const app = new Hono()14851486 class CustomError extends Error {1487 getResponse() {1488 return new Response('Custom Error', { status: 400 })1489 }1490 }14911492 app.get('/exception', () => {1493 throw new CustomError()1494 })14951496 it('Should return 401 response', async () => {1497 const res = await app.request('http://localhost/exception')1498 expect(res.status).toBe(400)1499 expect(await res.text()).toBe('Custom Error')1500 })1501 })1502})15031504describe('Error handling in middleware', () => {1505 const app = new Hono()
2001 unmodified lines
modified:src/types.ts+5 added-1 removed
89 unmodified lines
90 R extends HandlerResponse<any> = any91> = Handler<E, P, I, R> | MiddlewareHandler<E, P, I>9293export type NotFoundHandler<E extends Env = any> = (c: Context<E>) => Response | Promise<Response>9495export interface HTTPResponseError extends Error {96 getResponse: () => Response97}98export type ErrorHandler<E extends Env = any> = (95 err: Error,99 err: Error | HTTPResponseError,100 c: Context<E>101) => Response | Promise<Response>102103////////////////////////////////////////
1749 unmodified lines