{"version":3,"file":"express.mjs","names":[],"sources":["../../src/integrations/express.ts"],"sourcesContent":["import type * as http from 'node:http';\nimport { recordException } from '../instrumentations/exceptions/index.ts';\n\ninterface MiddlewareError extends Error {\n  status?: number | string;\n  statusCode?: number | string;\n  status_code?: number | string;\n  output?: {\n    statusCode?: number | string;\n  };\n}\n\ntype ExpressMiddleware = (\n  error: MiddlewareError,\n  req: http.IncomingMessage,\n  res: http.ServerResponse,\n  next: (error: MiddlewareError) => void,\n) => void;\n\nexport function expressErrorHandler(options?: {\n  shouldHandleError?: (error: MiddlewareError) => boolean;\n}): ExpressMiddleware {\n  return function mxlExpressErrorHandler(error, _req, _res, next): void {\n    const shouldHandle = options?.shouldHandleError ?? defaultShouldHandleError;\n    if (shouldHandle(error)) {\n      void recordException(error, {\n        attributes: {\n          'error.type': 'middleware',\n          'error.handled': false,\n        },\n      });\n    }\n    next(error);\n  };\n}\n\nexport function setupExpressErrorHandler(app: {\n  use: (middleware: ExpressMiddleware) => unknown;\n}) {\n  app.use(expressErrorHandler());\n}\n\nfunction getStatusCode(error: MiddlewareError): number {\n  const statusCode =\n    error.status ?? error.statusCode ?? error.status_code ?? error.output?.statusCode;\n  return statusCode ? Number.parseInt(String(statusCode), 10) : 500;\n}\n\nfunction defaultShouldHandleError(error: MiddlewareError) {\n  return getStatusCode(error) >= 500;\n}\n"],"mappings":";;AAmBA,SAAgB,oBAAoB,SAEd;CACpB,OAAO,SAAS,uBAAuB,OAAO,MAAM,MAAM,MAAY;EAEpE,KADqB,SAAS,qBAAqB,yBAAA,CAClC,KAAK,GACpB,gBAAqB,OAAO,EAC1B,YAAY;GACV,cAAc;GACd,iBAAiB;EACnB,EACF,CAAC;EAEH,KAAK,KAAK;CACZ;AACF;AAEA,SAAgB,yBAAyB,KAEtC;CACD,IAAI,IAAI,oBAAoB,CAAC;AAC/B;AAEA,SAAS,cAAc,OAAgC;CACrD,MAAM,aACJ,MAAM,UAAU,MAAM,cAAc,MAAM,eAAe,MAAM,QAAQ;CACzE,OAAO,aAAa,OAAO,SAAS,OAAO,UAAU,GAAG,EAAE,IAAI;AAChE;AAEA,SAAS,yBAAyB,OAAwB;CACxD,OAAO,cAAc,KAAK,KAAK;AACjC"}