Skip to content

feat(http): error-handling & fallback routes + HTML/XSS response utilities #352

Description

@pathosDev

Two related gaps in the HTTP module: no way to scope error handling to a route subtree or answer unmatched requests, and no safe way to build HTML responses.

Error handling & fallbacks

  • handleErrors(handler, child) — scoped exception handler (akka-http ExceptionHandler-style), sugar over the existing middleware Route kind; sees the original thrown error before the backend default mapping; returning null escalates.
  • fallback(handler) — root-only Route kind wired to each backend's setNotFound hook (method-agnostic). Capability check in bind(); single-fallback guard.
  • ServerBuilder.withErrorHandler(h) — server-wide last-resort handler wired to setErrorHandler. Requires unifying FastifyBackend so handler-thrown errors also reach it (today its per-route try/catch swallows them before setErrorHandler).
  • Shared defaultErrorResponse(err) helper; exports from src/http/index.ts.

HTML / XSS utilities (src/http/Html.ts)

  • escapeHtml(s), SafeHtml brand, html tagged template (auto-escapes interpolations, SafeHtml verbatim, arrays recursive), rawHtml escape hatch, completeHtml(status, body)text/html; charset=utf-8 + x-content-type-options: nosniff.
  • Out of scope here: sanitization of untrusted markup — tracked separately.

Notes

  • Pre-1.0 BREAKING (flag in CHANGELOG): Route/CompiledEndpoint unions widen; ServerBuilder gains a method; Fastify setErrorHandler now also receives handler errors.
  • Tests: HandleErrors, Fallback (3-backend matrix), ServerErrorHandler, Html. Docs EN+DE: http/error-handling, http/html-xss.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions