Errors
Every failure becomes the same JSON shape, and internal details never reach clients.
Every error response has this shape:
{
"error": { "type": "not_found", "message": "No todo found with id 'todo_x'." },
"status": 404
}The error object can also carry code, param and, for validation
failures, errors.
Throwing errors
Throw an HttpError from a handler, strategy or authorizer:
throw HttpError.notFound(`No todo found with id '${id}'.`);
throw HttpError.unauthorized('Invalid token.');
throw HttpError.forbidden();
throw HttpError.invalidRequest('`limit` must be at most 100.', 'limit');
throw HttpError.validation(zodError, 'body'); // 400 validation_error, one entry per issue
throw new HttpError({ type: 'conflict', message: 'A project with that name exists.', code: 'name_taken' });The status comes from the type, unless you pass status yourself:
type | Status |
|---|---|
invalid_request | 400 |
validation_error | 400 |
unauthorized | 401 |
forbidden | 403 |
not_found | 404 |
method_not_allowed | 405 |
conflict | 409 |
unprocessable_entity | 422 |
rate_limited | 429 |
internal_server_error | 500 |
Unexpected errors
Only HttpError messages are shown to clients. Requests that fail
validation arrive as an HttpError already.
Anything else that's thrown, such as a database error or a bug, becomes:
{ "error": { "type": "internal_server_error", "message": "An unexpected error occurred." }, "status": 500 }That way SQL, file paths or credentials in connection strings never leak. The original message and stack go to your logger.
That includes a ZodError from a handler parsing its own data, such as a
database row: it's a bug, not the client's fault. If the handler is validating
client input, throw HttpError.validation(zodError) instead.
Library errors
Errors your database library throws for client mistakes, such as a missing
record or a duplicate value, would otherwise be 500s. Pass mapError to turn
them into HttpErrors first:
import { mapPostgresError, mapPrismaError } from 'hono-bento/db';
mountSpecs(app, specs, { strategies, authorizer, deps, mapError: [mapPrismaError, mapPostgresError] });Each mapper returns an HttpError or undefined, and the first match wins.
You can write your own: (error: unknown) => HttpError | undefined. The
original error is still logged, as cause. See
Database for what the built-in
mappers cover.
Unmatched requests
mountSpecs answers requests that no route matches in the same shape: 405 method_not_allowed, with an Allow header, when the path exists under
another method, and 404 not_found otherwise. Pass handleNotFound: false to
keep your own notFound handler.
Logging and custom serialization
mountSpecs(app, specs, {
strategies,
authorizer,
logger: console, // any { info, warn, error }: 5xx errors log at `error`, the rest at `info`
mapError: mapPrismaError, // library errors to HttpErrors, before serialization
onError: (error, c) => c.json({ message: 'Something went wrong' }, 500),
});normalizeError(error) turns any thrown value into the standard body, if you
need the same shape elsewhere, such as in Hono's own app.onError.