The problem of catching incoming errors in an async error handler

9 Views Asked by At

I'm trying to create an error handler and everything is ok so far, but I'm running into a problem when I have errors in the prism but I can't give them next(error) like in a normal service controller. and I wouldn't want to keep using try-catch for this. What solutions do you have?

 if (websiteLink) throw next(new ErrorException({code: UNPROCESSABLE_ENTITY, message: EXISTS})); -> What do I do if I have errors from PrismaORM
/**
 *
 * @param {Error} err
 * @param {Request} req
 * @param {Response} res
 * @param {NextFunction} next
 * @return {void}
 */
export const errorHandler = (
    err: Error,
    req: Request,
    res: Response,
    next: NextFunction
) : void => {
    try {
        if (err instanceof ErrorException) {
            if (err.status !== null) {
                logger.error(err);
                sendResponse(res, err.message, err, false, err.status);
            }
            next();
        } else {
            logger.error(err);
            console.error("ERROR: ", err);
            sendResponse(res, SERVER_ERROR_MESSAGE, {}, false, SERVER_ERROR);
        }
    } catch (error) {
        sendResponse(res, SERVER_ERROR_MESSAGE, {}, false, SERVER_ERROR);
    }
};
0

There are 0 best solutions below