import { ApiErrorCode, createErrorResponse, getErrorMessage } from '../types.js';
/**
 * Install the global error handler that renders structured route errors.
 * Errors thrown with a `statusCode`/`body` (see route-helpers.ts) are sent
 * verbatim at that status; anything else becomes a 500 OPERATION_FAILED response.
 */
export function installRouteErrorHandler(app) {
    app.setErrorHandler((error, _req, reply) => {
        const statusCode = error.statusCode ?? 500;
        const body = error.body;
        if (body) {
            reply.code(statusCode).send(body);
        }
        else {
            reply.code(statusCode).send(createErrorResponse(ApiErrorCode.OPERATION_FAILED, getErrorMessage(error)));
        }
    });
}
//# sourceMappingURL=route-error-handler.js.map