I am having some issues with error handling using ExpressJS and Prisma. Whenever a Prisma Exception occurs, my entire Node application crashes, and I have to restart it. I have done some googling and have looked at the Prisma Docs for error handling, but I can't find any answers.
I know I could possibly use try and catch, but this feels unnecessary, as I could handle this much better with an error handler, especially when a lot of information on errors is passed through Prisma.
I have tried to implement the Express error handler like this:
// index.ts
import errorHandler from "./middleware/errorHandler";
...
server.use(errorHandler);
// errorHandler.ts
import { NextFunction, Response } from "express";
// ts-ignore because next function is required for some weird reason
// @ts-ignore
const errorHandler = (err: any, _: any, res: Response, next: NextFunction) => {
console.error(err.stack);
res.status(500).send("Internal Server Error");
};
export default errorHandler;
This works fine for normal errors, but doesn't execute for Prisma errors, but instead just crashes the Node application.
How can I implement an error handler so I can manage Prisma Expections?