Skip to content

Middleware on notFound / onError #5193

Description

@na-trium-144

What is the feature you are proposing?

Currently, onError and notFound only accept a single handler. It would be great if they could accept middlewares, similar to route definitions.

Use Case: running specific middleware (e.g., languageDetector) when rendering 404 page or error page, without registering it globally.
(For example you might not want to use languageDetector globally because it has the side effect of disabling the edge cache due to the Set-Cookie.)

const app = new Hono()

app.notFound(
  languageDetector(),
  (c) => {
    return c.html(render404Page(c.get("language")))
  }
)

app.onError(
  languageDetector(),
  (err, c) => {
    return c.html(renderErrorPage(c.get("language")))
  }
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions