import type { Response } from 'express'; import mongoose from 'mongoose'; import type { FrameworkRequest } from './HttpServer.ts'; import { HttpResponse } from './responses.ts'; /** The `message` of every framework 500 response. */ export declare const serverErrorMessage: (req: FrameworkRequest) => string; /** The `message` of every framework field-error (400) response. */ export declare const validationFailedMessage: (req: FrameworkRequest) => string; /** Levels the registry may log a handled error at (winston leveled methods). */ export type ErrorLogLevel = 'error' | 'warn' | 'info' | 'http' | 'verbose' | 'debug' | 'silly'; /** What a handler returns to produce a response; `null`/`undefined` = pass. */ export interface ErrorHandlerResult { status: number; body: unknown; /** Response headers, e.g. `{ 'Retry-After': '30' }` on a 429. */ headers?: Record; } /** A resolved error: the response to send, the entry's log level, and the legacy result fields. */ export interface ResolvedError extends ErrorHandlerResult { response: HttpResponse; logLevel: ErrorLogLevel; } /** * Answer a resolved error: its headers, status and JSON body. * @deprecated The framework sends `ResolvedError.response` through the response writer now; build one with `errorResultResponse`. */ export declare function sendErrorResult(res: Response, { status, body, headers }: ErrorHandlerResult): Response>; /** * A `{ status, body, headers }` result as the response `sendErrorResult` sent: * JSON, with a `Content-Type` header kept as the media type, and no body for * an undefined body or a status that never carries one. */ export declare function errorResultResponse({ status, body, headers, }: ErrorHandlerResult): HttpResponse; /** * @internal The response to send for a resolved error. A replaced * `resolveError` may return only the legacy fields; it is built from those. */ export declare function resolvedErrorResponse(resolved: ErrorHandlerResult & { response?: HttpResponse; }): HttpResponse; /** @internal Record that a registry handler failed for `req`, until `errorHandlerFailed` reads it. */ export declare function markErrorHandlerFailed(req: object): void; /** * @internal Did a registry handler fail for `req`? Its error answers the * generic 500, never a caller's fallback response. Kept per request, so * requests that throw the same error instance do not interfere. The read * clears the mark. */ export declare function errorHandlerFailed(req: object): boolean; /** The generic 500 answered for an error nothing in the registry resolved. */ export declare const serverErrorResponse: (req: FrameworkRequest) => import("./responses.ts").JsonResponse<500, { error: "SERVER_ERROR"; message: string; }, "application/json">; /** * What `registerErrorHandler` accepts: any `Error` subclass, including one that * widens `message` (the framework's `ValidationError` carries its field * payload there). Structural, so the core declarations need not import it. */ export type RegistrableError = Omit; /** * Maps an error to a response: a `{ status, body, headers? }` result, an * `HttpResponse` (for other content or full control), or `null`/`undefined` * to pass to the next entry. */ export type ErrorHandlerFn = (err: E, req: FrameworkRequest) => ErrorHandlerResult | HttpResponse | null | undefined | Promise; /** A registered (class, handler, level) triple as stored by the registry. */ export interface RegisteredErrorHandler { errorClass: abstract new (...args: never[]) => RegistrableError; handler: ErrorHandlerFn; logLevel: ErrorLogLevel; /** @internal Picks the level per error; wins over `logLevel`. */ logLevelFor?: (err: RegistrableError) => ErrorLogLevel; } /** * Sanitize an error for the RESOLVED-branch log line. A raw Mongoose * `ValidationError`'s `.message` (and `.stack` header) interpolates the * rejected `{VALUE}`, so `logger.warn(err)` would leak the submission into * logs — and whatever ships them (Sentry, retention) — even after the 400 * body was sanitized. Rebuild a fresh `Error` that reuses the per-path * `safeValidationMessage` texts under the safe static prefix * (` validation failed`); its stack is minted here, input-free. * A request `ValidationError` (its `message` is the field payload) becomes * `Request validation failed ( fields)`: a path can be a client-supplied * key, so neither paths, values nor per-field texts are logged. * Anything else passes through untouched. The UNRESOLVED 500 path deliberately * keeps logging the original error in full (a request `ValidationError` aside): * that branch is a server-side defect, and the developer needs every detail. */ export declare function toLoggableError(err: unknown): unknown; /** * Safety net for an escaped Mongoose `ValidationError`: returns a per-field * `{ : message }` map ONLY when every failing model path is a field the * client actually sent — the keys of the validated `request` ∪ `query`, minus * the framework-injected `contentType` discriminant (see * `ControllerManager.#wrapHandlerEntry`). A nested path (`profile.name`) * matches on its first segment and is reported under the full path (the * client owns that subtree). Any renamed/internal path → `null`, so the * caller keeps the honest 500 and never leaks a non-public field name. * * Each message is rebuilt by `safeValidationMessage` from the validation * `kind` + constraint — the raw `err.errors[path].message` is NEVER used, so * the submitted value can't ride into the response or the log. */ export declare function matchedClientValidationErrors(err: mongoose.Error.ValidationError, req: FrameworkRequest): Record | null; /** * Safety net for a STANDALONE Mongoose `CastError` — the shape a path param * produces (`findById(req.params.id)` with a malformed id). A `CastError` is a * sibling of `ValidationError`, not a subclass, so * {@link matchedClientValidationErrors} structurally cannot see it, and its * failing path is an internal model field (`_id`) that must never be echoed. * * Returns `{ : message }` only when the rejected value is one the * client actually supplied — matched by VALUE against the path params and the * validated body/query, then reported under that public input name. A cast * failure on a server-computed value matches nothing and returns `null`, so a * genuine server-side defect keeps its honest 500 instead of being blamed on * the caller. The message comes from {@link safeValidationMessage}, so the * rejected value never rides into the response. */ export declare function matchedClientCastError(err: mongoose.Error.CastError, req: FrameworkRequest): Record | null; /** * Framework built-in registry entries, checked AFTER any consumer-registered * handlers ("yours win"): * 1. `HttpError` → its own status, headers and * `body ?? { error?: code, message }`, message translated via `i18nKey`; * `verbose` (deliberate control flow, not a defect), `error` for a 5xx * (a server failure). * 2. Escaped Mongoose `ValidationError` → the safety net above; `warn` * (signals a route schema missing a constraint the model enforces). * 3. Standalone Mongoose `CastError` → {@link matchedClientCastError}; `warn` * (signals a path param or input reaching the model unvalidated). Declaring * a route `params:` schema turns the same failure into a 400 earlier, with * a message you control. */ export declare function builtInErrorHandlers(): RegisteredErrorHandler[];