UNPKG

@unito/integration-sdk

Version:

Integration SDK

56 lines (46 loc) 1.2 kB
import { Request, Response, NextFunction } from 'express'; import { Error as ApiError } from '@unito/integration-api'; import { HttpError } from '../httpErrors.js'; declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace Express { interface Locals { error?: ApiError; } } } function onError(err: Error, _req: Request, res: Response, next: NextFunction) { if (res.headersSent) { return next(err); } let error: ApiError; if (err instanceof HttpError) { error = { code: err.status.toString(), message: err.message, details: { stack: err.stack, }, }; } else { error = { code: '500', message: 'Oops! Something went wrong', originalError: { code: err.name, message: err.message, details: { stack: err.stack, }, }, }; } res.locals.error = structuredClone(error); // Keep the stack details in development for the Debugger if (process.env.NODE_ENV !== 'development') { delete error.details; delete error.originalError?.details; } res.status(Number(error.code)).json(error); } export default onError;