/**
 * Error handling utilities for The Order services
 */
/**
 * Custom application error class
 */
export class AppError extends Error {
    statusCode;
    code;
    details;
    constructor(statusCode, code, message, details) {
        super(message);
        this.statusCode = statusCode;
        this.code = code;
        this.details = details;
        this.name = 'AppError';
        Error.captureStackTrace(this, this.constructor);
    }
}
/**
 * Global error handler for Fastify
 */
export async function errorHandler(error, request, reply) {
    request.log.error({
        err: error,
        url: request.url,
        method: request.method,
        statusCode: error.statusCode || 500,
    });
    if (error instanceof AppError) {
        return reply.status(error.statusCode).send({
            error: {
                code: error.code,
                message: error.message,
                details: error.details,
            },
        });
    }
    // Handle validation errors
    if (error.validation) {
        return reply.status(400).send({
            error: {
                code: 'VALIDATION_ERROR',
                message: 'Validation failed',
                details: error.validation,
            },
        });
    }
    // Don't expose internal errors in production
    const isProduction = process.env.NODE_ENV === 'production';
    return reply.status(error.statusCode || 500).send({
        error: {
            code: 'INTERNAL_ERROR',
            message: isProduction ? 'Internal server error' : error.message,
            ...(isProduction ? {} : { stack: error.stack }),
        },
    });
}
/**
 * Create a standardized error response
 */
export function createErrorResponse(statusCode, code, message, details) {
    return new AppError(statusCode, code, message, details);
}
//# sourceMappingURL=error-handler.js.map