CI / build (push) Has been cancelled
Three capabilities that share the same request/route metadata:
- **Per-route upload limits** — `@Post('/x', { maxBodyBytes, maxParts })`
overrides the server-wide `limits` for one route (falls back to the
global default), mirroring the existing per-route `duplicates`.
- **Custom error rendering** — `createApp({ onError })` takes over how any
error becomes a response (HTML, RFC 7807, content-negotiated), covering
the whole surface: handler/step throws, 404, 405, 413, 400. Returns a
response or undefined to fall back to the default JSON. Also wires the
previously-unused `HttpError` `body` field so `throw new HttpError(409,
'conflict', { code })` renders a structured payload. Default behavior is
unchanged when no hook is set.
- **OpenAPI 3.1** — `app.openapi(info?)` projects the route graph into a
structural spec (paths, methods, path/query/header params, request-body
presence, systematic error responses); opt-in `GET /__openapi__` via
`devOpenapi`. Body/response schemas stay generic (Standard Schema exposes
no JSON Schema) — the full `@Produce` mode is post-beta.
Docs: new error-handling and OpenAPI guides, plus reference updates.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
import { isHttpError, Redirect, ValidationError } from './signals';
|
|
import type { TransformerFn } from './metadata';
|
|
import { flattenPath } from './standard-schema';
|
|
|
|
/** A ready-to-send HTTP response. */
|
|
export interface ErrorResponse {
|
|
status: number;
|
|
headers: Record<string, string>;
|
|
body: string;
|
|
}
|
|
|
|
/** The request a {@link ErrorRenderer} is given, to inspect (e.g. `headers.accept`) while rendering. */
|
|
export interface ErrorRequest {
|
|
method: string;
|
|
url: string;
|
|
headers: Record<string, string | string[] | undefined>;
|
|
}
|
|
|
|
/**
|
|
* User hook to render errors however you like (HTML, custom JSON, RFC 7807, …).
|
|
* Return a response to take over, or `undefined`/`void` to fall back to the default JSON.
|
|
*/
|
|
export type ErrorRenderer = (error: unknown, req: ErrorRequest) => ErrorResponse | undefined | void;
|
|
|
|
/** Default transformer: serialize a handler's return value as a JSON `200` response. */
|
|
export const JsonTransformer: TransformerFn = (value) => ({
|
|
status: 200,
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(value),
|
|
});
|
|
|
|
/**
|
|
* Map a thrown value to a JSON error response (422 for validation, `HttpError` status, else 500).
|
|
* An `HttpError` carrying a `body` renders that payload instead of the default `{ error: message }`.
|
|
*/
|
|
export function errorToResponse(error: unknown): ErrorResponse {
|
|
if (error instanceof ValidationError) {
|
|
return {
|
|
status: 422,
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify({
|
|
error: 'Validation failed',
|
|
source: error.source,
|
|
issues: error.issues.map((issue) => ({ path: flattenPath(issue), message: issue.message })),
|
|
}),
|
|
};
|
|
}
|
|
|
|
if (isHttpError(error)) {
|
|
const headers: Record<string, string> = { 'content-type': 'application/json' };
|
|
if (error instanceof Redirect) headers.location = error.location;
|
|
const payload = error.body !== undefined ? error.body : { error: error.message };
|
|
return { status: error.status, headers, body: JSON.stringify(payload) };
|
|
}
|
|
|
|
return {
|
|
status: 500,
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify({ error: 'Internal Server Error' }),
|
|
};
|
|
}
|
|
|
|
/** Render an error through the optional user {@link ErrorRenderer}, falling back to {@link errorToResponse}. */
|
|
export function renderError(error: unknown, req: ErrorRequest, onError?: ErrorRenderer): ErrorResponse {
|
|
return onError?.(error, req) ?? errorToResponse(error);
|
|
}
|