Spaces:
Configuration error

File size: 2,270 Bytes
023064b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { ErrorRequestHandler, RequestHandler } from 'express';
import { ZodError } from 'zod';
import { AppError, isAppError, NotFoundError, RequestValidationError } from '../lib/errors';
import { redactSecretsIn } from '../agentic/operations/security.js';
import { getRequestId, getRequestLogger } from './request-context';

function normalizeError(error: unknown): AppError {
    if (isAppError(error)) {
        return error;
    }

    if (error instanceof ZodError) {
        return new RequestValidationError(
            'Invalid request payload.',
            error.issues.map((issue) => ({
                path: issue.path.length > 0 ? issue.path.join('.') : '(root)',
                message: issue.message,
            })),
        );
    }

    if (error instanceof SyntaxError && 'body' in error) {
        return new RequestValidationError('Invalid JSON body.');
    }

    return new AppError('Internal server error');
}

export const notFoundHandler: RequestHandler = (req, _res, next) => {
    next(new NotFoundError(`Route not found: ${req.method} ${req.originalUrl}`));
};

export const errorHandler: ErrorRequestHandler = (error, _req, res, next) => {
    if (res.headersSent) {
        next(error);
        return;
    }

    const normalized = normalizeError(error);
    const logger = getRequestLogger(res);
    const requestId = getRequestId(res);
    // Redact any secret-shaped values from error details before logging or
    // returning them in the response (defense-in-depth; never leak keys).
    const safeDetails = normalized.details !== undefined ? redactSecretsIn(normalized.details) : undefined;
    const logContext = {
        code: normalized.code,
        requestId,
        statusCode: normalized.statusCode,
        details: safeDetails,
    };

    if (normalized.statusCode >= 500) {
        logger.error('request.failed', logContext, error);
    } else {
        logger.warn('request.rejected', logContext, error);
    }

    res.status(normalized.statusCode).json({
        success: false,
        error: normalized.expose ? normalized.message : 'Internal server error',
        code: normalized.code,
        requestId,
        ...(normalized.expose && safeDetails !== undefined ? { details: safeDetails } : {}),
    });
};