josephwoodward / GlobalExceptionHandlerDotNet

Exception handling as a convention in the ASP.NET Core request pipeline
MIT License
272 stars 32 forks source link

Doesn't handle custom exceptions in net.core 3.1 #41

Open ozonni opened 4 years ago

ozonni commented 4 years ago
builder.UseGlobalExceptionHandler(
                x =>
                {
                    x.ContentType = "application/json";
                    x.ResponseBody(s => JsonConvert.SerializeObject(new
                    {
                        Message = "An error occurred whilst processing your request"
                    }));

                    x.Map<EntityNotFoundException>().ToStatusCode(HttpStatusCode.NotFound);
                });

Having this configuration and throwing EntityNotFoundException, i'm getting HTTP 500

josephwoodward commented 4 years ago

Oh interesting. I'll have to check this out as it should work. I'm currently prepping some changes for 3.1 which should be released in the next few days.

ozonni commented 4 years ago

@JosephWoodward found it, i didn't configure this

x.ContentType = "application/json";
x.ResponseBody(s => JsonConvert.SerializeObject(new
{
    Message = "An error occurred whilst processing your request"
}));

After adding this part, i'm getting HTTP 404.

I'm not sure if this should be required, since i just wanted to return HTTP 404 without any JSON response body

josephwoodward commented 4 years ago

@ozonni Thanks for the update. What did your code look like before you added the above? Unless I'm wrong, it looks the same as the original sample you added when raising the issue?

ozonni commented 4 years ago

@JosephWoodward right :) mb i copied wrong code. Sorry for that

josephwoodward commented 4 years ago

Ah okay. None the less, you've highlighted that the following should really return a 404:

builder.UseGlobalExceptionHandler(x =>
{
    x.Map<EntityNotFoundException>().ToStatusCode(HttpStatusCode.NotFound);
});
andregizero commented 4 years ago

@JosephWoodward Is the release with pending changes for .NET Core 3.1 to be released soon?

josephwoodward commented 4 years ago

@andregizero Yes, it's almost ready.

steve-gombos commented 4 years ago

Any status on the changes for 3.1? Or if the current master can be pushed to nuget? There are some updates for loggers that would be awesome to integrate with.