Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/compose.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context } from './context'
import type { Env, ErrorHandler, Next, NotFoundHandler } from './types'
import type { Env, ErrorHandler, UnknownErrorHandler, Next, NotFoundHandler } from './types'

/**
* Compose middleware functions into a single function based on `koa-compose` package.
Expand All @@ -15,6 +15,7 @@ import type { Env, ErrorHandler, Next, NotFoundHandler } from './types'
export const compose = <E extends Env = Env>(
middleware: [[Function, unknown], unknown][] | [[Function]][],
onError?: ErrorHandler<E>,
onUnknownError?: UnknownErrorHandler<E>,
onNotFound?: NotFoundHandler<E>
): ((context: Context, next?: Next) => Promise<Context>) => {
return (context, next) => {
Expand Down Expand Up @@ -50,10 +51,13 @@ export const compose = <E extends Env = Env>(
try {
res = await handler(context, () => dispatch(i + 1))
} catch (err) {
context.error = err
if (err instanceof Error && onError) {
context.error = err
res = await onError(err, context)
isError = true
} else if (onUnknownError) {
res = await onUnknownError(err, context)
isError = true
} else {
throw err
}
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export class Context<
* })
* ```
*/
error: Error | undefined
error: unknown | undefined

#status: StatusCode | undefined
#executionCtx: FetchEventLike | ExecutionContext | undefined
Expand Down
22 changes: 20 additions & 2 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
OnHandlerInterface,
RouterRoute,
Schema,
UnknownErrorHandler,
} from './types'
import { COMPOSED_HANDLER } from './utils/constants'
import { getPath, getPathNoStrict, mergePath } from './utils/url'
Expand All @@ -41,6 +42,11 @@ const errorHandler: ErrorHandler = (err, c) => {
return c.text('Internal Server Error', 500)
}

const unknownErrorHandler: UnknownErrorHandler = (err, c) => {
console.error(err)
return c.text('Internal Server Error', 500)
}

type GetPath<E extends Env> = (request: Request, options?: { env?: E['Bindings'] }) => string

export type HonoOptions<E extends Env> = {
Expand Down Expand Up @@ -173,6 +179,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
getPath: this.getPath,
})
clone.errorHandler = this.errorHandler
clone.unknownErrorHandler = this.unknownErrorHandler
clone.#notFoundHandler = this.#notFoundHandler
clone.routes = this.routes
return clone
Expand All @@ -181,6 +188,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
#notFoundHandler: NotFoundHandler = notFoundHandler
// Cannot use `#` because it requires visibility at JavaScript runtime.
private errorHandler: ErrorHandler = errorHandler
private unknownErrorHandler: UnknownErrorHandler = unknownErrorHandler

/**
* `.route()` allows grouping other Hono instance in routes.
Expand Down Expand Up @@ -265,6 +273,11 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
return this
}

onUnknownError = (handler: UnknownErrorHandler): Hono<E, S, BasePath> => {
this.unknownErrorHandler = handler
return this
}

/**
* `.notFound()` allows you to customize a Not Found Response.
*
Expand Down Expand Up @@ -386,7 +399,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
if (err instanceof Error) {
return this.errorHandler(err, c)
}
throw err
return this.unknownErrorHandler(err, c)
}

#dispatch(
Expand Down Expand Up @@ -433,7 +446,12 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
: res ?? this.#notFoundHandler(c)
}

const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler)
const composed = compose(
matchResult[0],
this.errorHandler,
this.unknownErrorHandler,
this.#notFoundHandler
)

return (async () => {
try {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export type ErrorHandler<E extends Env = any> = (
c: Context<E>
) => Response | Promise<Response>

export type UnknownErrorHandler<E extends Env = any> = (
err: unknown,
c: Context<E>
) => Response | Promise<Response>

////////////////////////////////////////
////// //////
////// HandlerInterface //////
Expand Down
Loading