/** * Central error handling and sanitization for the LiteLLM backend. * * Ensures that: * - Upstream auth failures (401/403) don't log out Backstage sessions * - Upstream 5xx errors are generic and don't leak implementation details * - Upstream 4xx errors are sanitized and capped * - Non-JSON error bodies are not embedded in responses * - All error logging happens server-side only */ import { Response } from 'express'; import { LoggerService } from '@backstage/backend-plugin-api'; /** * Type guard: narrows unknown to {status: number; body: Record}. * Used to detect custom errors that have status and body properties. */ export declare function isHttpishError(error: unknown): error is { status: number; body: Record; }; /** * Safely extract a message string from any error type. * Returns empty string if no message can be extracted. */ export declare function errorMessage(error: unknown): string; /** * Sanitizes a message by: * - Redacting Bearer tokens and sk- keys * - Stripping HTML tags * - Collapsing consecutive whitespace * - Truncating to 500 characters * * @param msg The raw message (may contain HTML, newlines, secrets, etc.) * @returns A safe, printable message */ export declare function sanitizeUpstreamMessage(msg: string): string; /** * Maps any error type to an HTTP response shape {status, body}. * * Behaviour: * - LiteLLMUpstreamError 401/403 → 502 generic (prevent session logout) * - LiteLLMUpstreamError 5xx or network → 502 generic * - LiteLLMUpstreamError 4xx (other) → 400 with sanitized message + param if present * - KeyServiceError / ProvisioningError / objects with status+body → pass through * - Backstage-style errors (name: InputError/NotAllowedError/NotFoundError/ConflictError) → map to 400/403/404/409 * - Anything else → 500 (message logged server-side, not returned) * * @returns {status, body, logLevel} for HTTP response; undefined _logMessage is only present for 500s */ export declare function toHttpError(error: unknown): { status: number; body: Record; logLevel: 'debug' | 'info' | 'warn' | 'error'; _logMessage?: string; }; /** * Express error handler helper. Logs the real error server-side and responds * via toHttpError. * * @param res Express Response object * @param error The caught error (any type) * @param logger Logger instance with error/warn/info/debug methods * @param context Contextual label for the log entry (e.g. 'fetch user info') */ export declare function sendError(res: Response, error: unknown, logger: LoggerService, context: string): void;