Creating a Global Error Handler in NestJS using Prisma and reducing redundancy
Tags: nestjs, prisma, webdev
Published: 2023-09-26 08:22:00

In our NestJS Server, the way we handle errors has primarily revolved around the use of try-catch clauses. While this method has served us well, it has become increasingly clear that it leads to redundancy, especially when specifying error codes for various types of requests, such as POST, GET, UPDATE, or DELETE. This redundancy in error handling has prompted us to seek a more efficient and unified solution.

In a recent project, I have devised a more effective approach to error handling by implementing Exception Filters with dynamic error mapping. This approach not only simplifies error handling but also unifies the handling of HTTP errors and Prisma Known Request Errors, combining their respective error codes and messages into a single, centralized Exception Filter.

By consolidating these errors and their associated messages, we can significantly enhance the error-handling process and reduce redundancy in our codebase. This streamlined approach not only improves the maintainability of our server but also enhances the overall user experience by providing consistent error handling throughout our application.

For instance I have several error codes here from Prisma.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hc1jchmfc1dvbvop6lns.png)



// You can add more, the Error Codes are in the Prisma documentation`


In order for us to catch these errors respectively we will use the @Catch clause from NestJS.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrbnyi9gzjpr36q96dm1.png)





These types of errors that are being caught here are mostly PrismaClientKnownRequestError

Each Request that has a corresponding prisma error will be handled here in the Global Exception Filter, now for it to communicate with the error mapping we will use conditionals.




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jebh3tupph8c7at1qr3u.png)





Now the error mapping is used to display a formatted error message which points out on what path does it occur and its error code.

Now you might be wondering, how about handling HTTP Errors aside from catching only Prisma Errors?

That’s where our else statement comes in.




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9t7pcev0oe36myaurqz.png)



To ensure our global exception filter catches and handles errors effectively, we need to include it in our app module. By doing so, it will be positioned at the top of the error-handling stack, making error management in your NestJS application a breeze.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07j26451r5ddfe6y0cii.png)




In conclusion, handling errors gracefully is an essential part of web development. With the combination of Prisma and NestJS, you can create robust error handling mechanisms that improve user experiences and help you troubleshoot issues effectively. By implementing the techniques discussed in this article, you’ll be better equipped to handle errors in your NestJS applications.