{"version":3,"file":"error.mjs","names":[],"sources":["../../src/middlewares/error.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport type { HttpError } from 'http-errors';\nimport _ from 'lodash';\n\nimport type { VerdaccioError } from '@verdaccio/core';\nimport { API_ERROR, HTTP_STATUS } from '@verdaccio/core';\nimport type { Logger } from '@verdaccio/types';\n\nimport type { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';\n\nconst debug = buildDebug('verdaccio:middleware:error');\n\nexport const handleError = (logger: Logger) =>\n  function handleError(\n    err: HttpError,\n    req: $RequestExtend,\n    res: $ResponseExtend,\n    next: $NextFunctionVer\n  ) {\n    debug('error handler init');\n    if (_.isError(err)) {\n      debug('is native error');\n      if (err.code === 'ECONNABORT' && res.statusCode === HTTP_STATUS.NOT_MODIFIED) {\n        return next();\n      }\n      if (_.isFunction(res.locals.report_error) === false) {\n        debug('is locals error report ref');\n        // in case of very early error this middleware may not be loaded before error is generated\n        // fixing that\n        errorReportingMiddleware(logger)(req, res, _.noop);\n      }\n      debug('set locals error report ref');\n      res.locals.report_error(err);\n    } else {\n      // Fall to Middleware.final\n      debug('no error to report, jump next layer');\n      return next(err);\n    }\n  };\n\n// Middleware\nexport const errorReportingMiddleware = (logger: Logger) =>\n  function errorReportingMiddleware(\n    req: $RequestExtend,\n    res: $ResponseExtend,\n    next: $NextFunctionVer\n  ): void {\n    debug('error report middleware start');\n    res.locals.report_error =\n      res.locals.report_error ||\n      function (err: VerdaccioError): void {\n        if (err.status && err.status >= HTTP_STATUS.BAD_REQUEST && err.status < 600) {\n          debug('is error > 409 %o', err?.status);\n          if (_.isNil(res.headersSent) === false) {\n            debug('send status %o', err?.status);\n            res.status(err.status);\n            debug('next layer %o', err?.message);\n            next({ error: err.message || API_ERROR.UNKNOWN_ERROR });\n          }\n        } else {\n          debug('is error < 409 %o', err?.status);\n          logger.error({ err: err }, 'unexpected error: @{!err.message}\\n@{err.stack}');\n          if (!res.status || !res.send) {\n            // TODO: decide which debug keep\n            logger.error('this is an error in express.js, please report this');\n            debug('this is an error in express.js, please report this, destroy response %o', err);\n            res.destroy();\n          } else if (!res.headersSent) {\n            debug('send internal error %o', err);\n            res.status(HTTP_STATUS.INTERNAL_ERROR);\n            next({ error: API_ERROR.INTERNAL_SERVER_ERROR });\n          } else {\n            // socket should be already closed\n            debug('this should not happen, otherwise report %o', err);\n          }\n        }\n      };\n\n    debug('error report middleware end (skip next layer) next()');\n    next();\n  };\n"],"mappings":";;;;AAUA,IAAM,QAAQ,WAAW,4BAA4B;AAErD,IAAa,eAAe,WAC1B,SAAS,YACP,KACA,KACA,KACA,MACA;CACA,MAAM,oBAAoB;CAC1B,IAAI,EAAE,QAAQ,GAAG,GAAG;EAClB,MAAM,iBAAiB;EACvB,IAAI,IAAI,SAAS,gBAAgB,IAAI,eAAe,YAAY,cAC9D,OAAO,KAAK;EAEd,IAAI,EAAE,WAAW,IAAI,OAAO,YAAY,MAAM,OAAO;GACnD,MAAM,4BAA4B;GAGlC,yBAAyB,MAAM,CAAC,CAAC,KAAK,KAAK,EAAE,IAAI;EACnD;EACA,MAAM,6BAA6B;EACnC,IAAI,OAAO,aAAa,GAAG;CAC7B,OAAO;EAEL,MAAM,qCAAqC;EAC3C,OAAO,KAAK,GAAG;CACjB;AACF;AAGF,IAAa,4BAA4B,WACvC,SAAS,yBACP,KACA,KACA,MACM;CACN,MAAM,+BAA+B;CACrC,IAAI,OAAO,eACT,IAAI,OAAO,gBACX,SAAU,KAA2B;EACnC,IAAI,IAAI,UAAU,IAAI,UAAU,YAAY,eAAe,IAAI,SAAS,KAAK;GAC3E,MAAM,qBAAqB,KAAK,MAAM;GACtC,IAAI,EAAE,MAAM,IAAI,WAAW,MAAM,OAAO;IACtC,MAAM,kBAAkB,KAAK,MAAM;IACnC,IAAI,OAAO,IAAI,MAAM;IACrB,MAAM,iBAAiB,KAAK,OAAO;IACnC,KAAK,EAAE,OAAO,IAAI,WAAW,UAAU,cAAc,CAAC;GACxD;EACF,OAAO;GACL,M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