Exception Handling in .Net Core Web APIs

Exception Handling in .Net Core Web APIs

Exception handling was very important when it comes to the development of any web API.

Exception handling is a programming technique that allows you to deal with unexpected errors that occur during the execution of your program. When an exception is thrown, the normal flow of execution is interrupted and the exception is handled by a special block of code called an exception handler.

The very old way of handling the Exceptions in the .Net framework is using the try, catch blocks, but using this try, catch on across the project files is error-prone and also not good for maintainability. Later the release of the updated.Net core framework, the concept of Global Exceptions came into existence, with the use of this new feature, the exceptions are handled at single place when the call was initially made.

So what is the Global Exceptions Handling? Global exception handling in .NET Core Web API is a way to handle exceptions that occur in your API. It is implemented by creating a global exception handler that catches all exceptions that are not handled by other code. This can be useful for debugging and logging errors, as well as for providing a consistent error-handling experience to your users.

To create a global exception handler in .NET Core Web API, you can use the UseExceptionHandler middleware. This middleware will catch all exceptions that are not handled by other middleware and will handle them in a consistent way.

You got a situation whenever an exception was occurred in any API call as a developer you want to see its entire exception details but not for the end-user. For the end user, the API response should display a valid message but not the entire exception stack details. Hence it is not a great user experience if we show all the details to the end user.

By showing all the details to the end-user if the user is tech-savvy they easily know about the project tech stack like from which lang/stach the application was built and also some exception messages display the IP address or API full-length Url which is very risky.

In order to avoid this kind of thing the Global Exception with the help of Middleware service in the.Net core is very useful. Below is the implementation for this :

Create a project on .Net core 6 or above and in the launchSettings.json maintain the two profiles one is for Development and one for Production.

Next, Create a Custom Response class like below :

Finally, create an Exception Middleware class and configure it in the startup.cs file. the InvokeAysnc was for each and every API call, under the HandleException method the logic was implemented based on the environment selected.

The complete project code is available on Github: https://github.com/DileepSreepathi/GlobalExceptions

Thank you for reading until the end. Please consider following the writer and this publication. Visit Stackademic to find out more about how we are democratizing free programming education around the world.

Did you find this article valuable?

Support Dileep Sreepathi by becoming a sponsor. Any amount is appreciated!