Spaces:
Running
Running
| const { env } = require('../configs/env'); | |
| const { logError } = require('./errorLogger'); | |
| const HTTP_CODE_MAP = { | |
| 400: 'BAD_REQUEST', | |
| 401: 'UNAUTHORIZED', | |
| 403: 'FORBIDDEN', | |
| 404: 'NOT_FOUND', | |
| 409: 'CONFLICT', | |
| 413: 'PAYLOAD_TOO_LARGE', | |
| 422: 'UNPROCESSABLE_ENTITY', | |
| 429: 'RATE_LIMIT_EXCEEDED', | |
| 503: 'SERVICE_UNAVAILABLE', | |
| }; | |
| const getCode = (statusCode) => | |
| HTTP_CODE_MAP[statusCode] || (statusCode >= 500 ? 'INTERNAL_SERVER_ERROR' : 'REQUEST_FAILED'); | |
| const asyncController = (handler, { defaultStatus = 500, onError } = {}) => async (req, res, next) => { | |
| try { | |
| await handler(req, res, next); | |
| } catch (err) { | |
| if (res.headersSent) { | |
| return next(err); | |
| } | |
| if (typeof onError === 'function') { | |
| const handled = await onError(err, req, res, next); | |
| if (handled) return; | |
| } | |
| const statusCode = | |
| Number.isInteger(err?.statusCode) && err.statusCode >= 400 | |
| ? err.statusCode | |
| : res.statusCode >= 400 | |
| ? res.statusCode | |
| : defaultStatus; | |
| const code = getCode(statusCode); | |
| if (statusCode >= 500) { | |
| logError(err, { req, statusCode, code }); | |
| } | |
| const message = env.isProduction && statusCode >= 500 | |
| ? 'An internal error occurred' | |
| : (err.message || 'Request failed'); | |
| res.status(statusCode).json({ success: false, message, code }); | |
| } | |
| }; | |
| module.exports = { asyncController }; | |