http/dev_route_error_check.ts

The DEV-only error check: an outer middleware that logs every JSON error response its route doesn't declare.

Mounted outermost by create_app_server, so it sees what every layer answers — a middleware's refusal (the body limit, the origin check, the auth guards) as much as a handler's or the error catch's. After the request it finds the route the request reached (create_route_lookup: Hono's own router over the route specs in mount order, HEAD read as GET) and checks the response against that route's error map (check_route_error): an error status the map lacks, or a body off the status's schema, is logged. A 500 internal_error and a 503 service_unavailable are always accepted, a JSON-RPC error envelope on a jsonrpc route is not a REST error body, a non-JSON response is skipped, and so is a request no route spec matches: the unmatched-route handler's 405 and 404 (server/unmatched_route.ts) answer for no route, so no route's map can hold them.

Logs, never throws — a throw inside it, its logger's included, is swallowed — and leaves the response unchanged. Outside DEV create_dev_route_error_check returns null and nothing is mounted.

view source

Declarations
#

4 declarations

create_dev_route_error_check
#

http/dev_route_error_check.ts view source

(options: DevRouteErrorCheckOptions): MiddlewareHandler | null import {create_dev_route_error_check} from '@fuzdev/fuz_app/http/dev_route_error_check.js';

Build the DEV error check (module doc), or null outside DEV.

options

the routes, their error maps, and the logger

returns

(MiddlewareHandler) | null

the middleware to mount outermost, or null in production

throws

  • Error - when `route_error_maps` lacks a route of `route_specs` (`get_route_error_map`)

create_route_lookup
#

http/dev_route_error_check.ts view source

<TRoute extends { readonly method: string; readonly path: string; }, T>(routes: readonly TRoute[], to_value: (route: TRoute) => T): (method: string, path: string) => T | undefined import {create_route_lookup} from '@fuzdev/fuz_app/http/dev_route_error_check.js';

A lookup of the route a request reaches, by Hono's own router — the SmartRouter over a RegExpRouter and a TrieRouter that new Hono() builds: the routes are added in mount order, and a request's first match is the route Hono dispatches it to — HEAD read as GET, which Hono answers it with.

Its router holds only the route specs, while the app's also holds every middleware and non-spec route (WS upgrades, static serving), which can put the app on a different router than the lookup's — a TrieRouter where the lookup has a RegExpRouter. Where the two differ the cost is a missed DEV log, and rarely a wrong route.

The path is matched as the app's router matches it: the raw request path (get_request_path), nothing percent-decoded.

routes

the routes, in the order they were mounted

type readonly TRoute[]

to_value

what to keep beside each route

type (route: TRoute) => T

returns

(method: string, path: string) => T | undefined

a function from a request's method and path to the matched route's value, or undefined when none matches

generics

create_route_lookup<TRoute extends { readonly method: string; readonly path: string }, T>
TRoute
constraint { readonly method: string; readonly path: string }
T

DevRouteErrorCheckOptions
#

DevRouteErrorCheckSpec
#

Depends on
#

Imported by
#