title: Handle Prisma errors with Express
body: <p>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 <a href="https://www.prisma.io/docs/concepts/components/prisma-client/handling-exceptions-and-errors" rel="nofollow noreferrer">Prisma Docs</a> for error handling, but I can't find any answers.</p>
  <p>I know I could possibly use <code>try</code> and <code>catch</code>, 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.</p>
  <p>I have tried to implement the Express error handler like this:</p>
  <pre><code>// index.ts
  
  import errorHandler from &quot;./middleware/errorHandler&quot;;
  ...
  server.use(errorHandler);
  
  // errorHandler.ts
  
  import { NextFunction, Response } from &quot;express&quot;;
  
  // ts-ignore because next function is required for some weird reason
  // @ts-ignore
  const errorHandler = (err: any, _: any, res: Response, next: NextFunction) =&gt; {
      console.error(err.stack);
      res.status(500).send(&quot;Internal Server Error&quot;);
  };
  
  export default errorHandler;
  </code></pre>
  <p>This works fine for normal errors, but doesn't execute for Prisma errors, but instead just crashes the Node application.</p>
  <p>How can I implement an error handler so I can manage Prisma Expections?</p>