Exception Handling in ASP.NET Core Web API
💻 coding

Exception Handling in ASP.NET Core Web API

2 min read 384 words
2 min read
ShareWhatsAppPost on X
  • 1ASP.NET Core Web API requires a different approach for exception handling compared to regular ASP.NET Web API.
  • 2Using middleware for exception handling provides full control and can catch exceptions from both application and action filters.
  • 3Register the exception handling middleware before MVC in the Startup class to ensure proper error management.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"ASP.NET Core Web API requires a different approach for exception handling compared to regular ASP.NET Web API."

Exception Handling in ASP.NET Core Web API

Question:

I started using ASP.NET Core for my new REST API project after using regular ASP.NET Web API for many years. I don't see a good way to handle exceptions in ASP.NET Core Web API. I tried to implement exception handling filter/attribute:

public class ErrorHandlingFilter : ExceptionFilterAttribute
{
 public override void OnException(ExceptionContext context)
 {
 HandleExceptionAsync(context);
 context.ExceptionHandled = true;
 }

 private static void HandleExceptionAsync(ExceptionContext context)
 {
 var exception = context.Exception;

 if (exception is MyNotFoundException)
 SetExceptionResult(context, exception, HttpStatusCode.NotFound);
 else if (exception is MyUnauthorizedException)
 SetExceptionResult(context, exception, HttpStatusCode.Unauthorized);
 else if (exception is MyException)
 SetExceptionResult(context, exception, HttpStatusCode.BadRequest);
 else
 SetExceptionResult(context, exception, HttpStatusCode.InternalServerError);
 }

 private static void SetExceptionResult(
 ExceptionContext context, 
 Exception exception, 
 HttpStatusCode code)
 {
 context.Result = new JsonResult(new ApiResponse(exception))
 {
 StatusCode = (int)code
 };
 }
}

And here is my Startup filter registration:

services.AddMvc(options =>
{
 options.Filters.Add(new AuthorizationFilter());
 options.Filters.Add(new ErrorHandlingFilter());
});

The issue I was having is that when exception occurred in my AuthorizationFilter it's not being handled by ErrorHandlingFilter. I was expecting it to be caught there just like it worked with old ASP.NET Web API.

So how can I catch all application exceptions as well as any exceptions from Action Filters?

Solution:

Exception Handling Middleware

After many experiments with different exception handling approaches I ended up using middleware. It worked the best for my ASP.NET Core Web API application. It handles application exceptions as well as exceptions from filters and I have full control over exception handling and creating a response json. Here is my exception handling middleware:

public class ErrorHandlingMiddleware
{
 private readonly RequestDelegate next;

 public ErrorHandlingMiddleware(RequestDelegate next)
 {
 this.next = next;
 }

 public async Task Invoke(HttpContext context /* other dependencies */)
 {
 try
 {
 await next(context);
 }
 catch (Exception ex)
 {
 await HandleExceptionAsync(context, ex);
 }
 }

 private static Task HandleExceptionAsync(HttpContext context, Exception exception)
 {
 var code = HttpStatusCode.InternalServerError; // 500 if unexpected

 if (exception is MyNotFoundException) code = HttpStatusCode.NotFound;
 else if (exception is MyUnauthorizedException) code = HttpStatusCode.Unauthorized;
 else if (exception is MyException) code = HttpStatusCode.BadRequest;

 var result = JsonConvert.SerializeObject(new { error = exception.Message });
 context.Response.ContentType = "application/json";
 context.Response.StatusCode = (int)code;
 return context.Response.WriteAsync(result);
 }
}

Register it before MVC in Startup class:

app.UseMiddleware(typeof(ErrorHandlingMiddleware));

app.UseMvc();

Here is an example of exception response:

{ "error": "Authentication token is not valid." }

You can add stack trace, exception type name, error codes or anything you want to it. Very flexible.

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 28 November 2018 · 2 min read · 384 words

Part of AskGif Blog · coding

You might also like

Exception Handling in ASP.NET Core Web API | AskGif Blog