{"version":3,"sources":["../src/error.ts"],"sourcesContent":["import {\n    Request, \n    Response\n} from \"express\";\nimport { inherits } from \"util\";\nimport log from \"loglevel\";\nimport { prettyJson } from \"@agentic-profile/common\";\n\n\nfunction errorCodeToStatusCode( code: any ) {\n    if( !code || !Array.isArray(code) )\n        return 500;\n    const parts = code as any[];\n    if( parts.length === 0 || parts.some(e=>Number.isFinite(e) !== true) )\n        return 500;\n\n    let result = parts[0]*100;\n    if( parts.length > 1 )\n        result += parts[1];\n\n    return result;\n}\n\n// Use this method when we have an Error object\nexport function signalError( req: Request, res: Response, err:any ) {\n    console.log( 'signalError', err );\n    const { code, details, name, message, stack, statusCode, cause } = err;\n    \n    // Handle custom error types that have a statusCode property (like ScrapingDogAPIError)\n    let httpStatusCode: number;\n    if (statusCode && typeof statusCode === 'number') {\n        httpStatusCode = statusCode;\n    } else {\n        httpStatusCode = errorCodeToStatusCode(code);\n    }\n\n    const msg = cause ? `${message ?? name} [cause] ${cause.message}` : message ?? name;\n    \n    const failure = {\n        code,\n        message: msg,\n        details: details ?? stack?.split(/\\n/).map((e:string)=>e.trim()).slice(0,7)\n    }\n    if( cause )\n        (failure as any).cause = cause;\n\n    logFailure( req,failure, err );\n    res.status( httpStatusCode ).json({ failure });\n}\n\nexport function logFailure( req: Request, failure: any, err?: any ) {\n    const auth = (req as any).auth;\n    log.error( 'ERROR:', prettyJson({\n        url: req.originalUrl,\n        headers: req.headers,\n        auth,\n        body: req.body,\n        failure,\n        errorMessage: err?.message\n    }) );\n}\n\nexport class ServerError extends Error {\n    code: number[];           // Array of HTTP status codes\n    details: string[] | undefined;  // Optional technical or support details\n\n    // code: []\n    // messagge: Human readable\n    // details: [] Tech support understandable\n    constructor(code: number[], message: string, details?: string[]) {\n        super(message);       // Call the parent class constructor\n        Error.captureStackTrace(this, this.constructor); // Attach the stack trace\n        this.name = this.constructor.name;  // Set the error name\n\n        this.code = code;     // Assign the provided status code(s)\n        this.details = details;  // Assign the provided details\n    }\n}\n\n//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