Calling 'AddJsonApi' bypass all the exception handler middlewares
See original GitHub issueDescription
Before using JsonApiDotNetCore, I had an ASP.NET Core middleware that had the responsibility to reformat all exceptions that occured in my controllers. This middleware was almost identical to this one.
As soon as I started to use JsonApiDotNetCore by calling AddJsonApi
, my middleware stopped working. This is because AddJsonApi registers an exception filter:
This exception filter handles all exceptions. My middleware is therefore unable to detect the exception.
Is there a way to disable this exception filter or to limit this exception filter to the routes that to use JSONAPI? My routes that don’t use JSONAPI should still use my exception middleware.
Environment
- JsonApiDotNetCore Version: 3.1.0.0
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
c# - Exception handler middleware not catching
In my case app.UseDeveloperExceptionPage(); was written in the Startup after the exception handler middleware. The fix was simply by moving the ...
Read more >Global Error Handling in ASP.NET Core Web API
In this article, we are going to handle errors by using a try-catch block first and then rewrite our code by using built-in...
Read more >Handle errors in ASP.NET Core web APIs
In Program.cs , call UseExceptionHandler to add the Exception Handling Middleware: C# Copy. var app = builder.Build(); app.UseHttpsRedirection ...
Read more >Exception Middleware in .NET Core Applications
Exception middleware is a feature in .NET Core that allows you to centralize the handling of errors and exceptions that may occur in...
Read more >Exception Handler | FastEndpoints
FastEndpoints has a built-in exception handler to help log uncaught errors in a convenient manner.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Another option would be to just inspect the ContentType of the request and only handle failures for application/vnd.api+json
Providing my own exception filter sure is a powerful option. But I feel that calling
AddJsonApi
should not change the behavior of an existing code base (which it currently does). New users of JsonApiDotNetCore won’t typically dig in the details of how this call affects the ASP.NET Core pipeline. Instead, JsonApiDotNetCore should, in my opinion, be as friction free as possible. I would suggest to add something like the following at the beginning ofJsonApiExceptionFilter.OnException
:What do you think of the idea?
In the mean time, I have no choice but to manually remove the JsonApiExceptionFilter from the list of filters added to MVC.