import type { Elysia, Context } from "elysia"; /** * Error Handler Configuration Options */ export interface ErrorHandlerOptions { /** Include stack traces in error responses (default: false in production) */ includeStack?: boolean; /** Custom error logger function */ logger?: (error: Error, context: Context) => void; /** Custom error response formatter */ formatter?: (error: Error, context: Context) => any; /** Environment (auto-detected if not provided) */ environment?: "development" | "production" | "test"; } /** * Standard error response format */ export interface ErrorResponse { error: string; message: string; statusCode: number; timestamp: string; path: string; stack?: string; details?: any; } /** * Configures global error handling middleware for the Elysia application * * This middleware provides comprehensive error handling capabilities including: * * Features: * - Automatic error catching and logging * - Standardized error response format * - Stack trace inclusion (dev mode only) * - HTTP status code mapping * - Request correlation for debugging * - Security: Prevents sensitive information leakage * - Custom error logging integration * * Error Types Handled: * - Validation errors (400 Bad Request) * - Authentication errors (401 Unauthorized) * - Authorization errors (403 Forbidden) * - Not found errors (404 Not Found) * - Internal server errors (500 Internal Server Error) * - Custom application errors * * Security Considerations: * - Never exposes sensitive error details in production * - Sanitizes error messages to prevent information disclosure * - Logs full error details server-side for debugging * - Prevents stack trace leakage in production * * Compliance: * - ISO/IEC 25010:2023 Reliability.Fault Tolerance * - OWASP Security Logging and Monitoring * * @param app - The Elysia application instance to configure * @param options - Error handling configuration options * * @returns {void} * * @example * ```typescript * import { Elysia } from 'elysia'; * import { setupErrorHandler } from './middleware/error-handler.middleware'; * * const app = new Elysia(); * * // Default configuration (recommended) * setupErrorHandler(app); * * // Custom configuration * setupErrorHandler(app, { * includeStack: process.env.NODE_ENV === 'development', * logger: (error, ctx) => { * console.error(`Error at ${ctx.path}:`, error); * // Send to error tracking service (Sentry, DataDog, etc.) * } * }); * * // Custom error response format * setupErrorHandler(app, { * formatter: (error, ctx) => ({ * success: false, * error: error.message, * code: getStatusCode(error), * requestId: ctx.request.headers.get('x-request-id') * }) * }); * ``` */ export declare function setupErrorHandler(app: Elysia, options?: ErrorHandlerOptions): void; /** * Custom error classes for common scenarios */ export declare class ValidationError extends Error { details?: any | undefined; statusCode: number; constructor(message: string, details?: any | undefined); } export declare class UnauthorizedError extends Error { statusCode: number; constructor(message?: string); } export declare class ForbiddenError extends Error { statusCode: number; constructor(message?: string); } export declare class NotFoundError extends Error { statusCode: number; constructor(message?: string); } export declare class ConflictError extends Error { statusCode: number; constructor(message?: string); } export declare class RateLimitError extends Error { retryAfter?: number | undefined; statusCode: number; constructor(message?: string, retryAfter?: number | undefined); } export declare class InternalServerError extends Error { statusCode: number; constructor(message?: string); } //# sourceMappingURL=error-handler.middleware.d.ts.map