UNPKG

@geniucode/common-utils

Version:

Common utils

68 lines 2.32 kB
import * as ErrorList from '../errors/index'; export const errorHandlerV2 = (err, req, res, next) => { const defaultStatusCode = 500; // General Error Status Code when the type of error is Unknown let statusCode = null; // General Error Status Code when the type of error is Unknown const msg = err?.message; if (err instanceof ErrorList.DuplicatedIdConflictError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.BadRequestError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.DatabaseConnectionError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.InternalServerError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.LargePayloadError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.NotFoundError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.ItemNotFoundError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.RequestValidationError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.RequestValidationWithMessageError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.ForbiddenError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.WrongHttpMethodError) { statusCode = err?.getStatusCode(); } else if (err instanceof ErrorList.RequestValidationErrorAsArray) { const stack = err?.stack; statusCode = err?.getStatusCode(); err = err.serializeErrors(); err.stack = stack; } else if (err instanceof ErrorList.CustomError) { statusCode = err?.getStatusCode(); } else { } statusCode = statusCode || defaultStatusCode; return { status: false, devCode: null, statusCode, msg, errors: err, stack: err?.stack, }; // res.status(statusCode).send({ // status: false, // devCode: null, // statusCode, // msg, // errors: err, // error.status_code // stack: err?.stack, // }); }; //# sourceMappingURL=error-handler-v2.js.map