proudmonkey / AutoWrapper

A simple, yet customizable global exception handler and Http response wrapper for ASP.NET Core APIs.
MIT License
679 stars 82 forks source link

new ApiException("Test", 404) always returning 500 #65

Closed ryanfaricy closed 4 years ago

ryanfaricy commented 4 years ago

Hello,

First of all, thanks for AutoWrapper, it's a joy and breeze to use.

In my new .NET Core Web API app, I want to throw an exception if an object wasn't found in the database, for example. But in my controller, when I do:

throw new ApiException("test", 404);

I receive:

{
    "isError": true,
    "type": "https://httpstatuses.com/500",
    "title": "Internal Server Error",
    "status": 500,
    "detail": "test",
    "instance": "/api/v1.0/notifications",
    "extensions": {},
    "errors": {
        "message": "test",
        "type": "ApiException",
        "source": "PM.Core.API",
        "raw": "<snip>"
    }
}

My result has a status code of 500 when I was expecting it to be a 404. Here is the configuration definition in Startup.cs:

            app.UseApiResponseAndExceptionWrapper(new AutoWrapperOptions
                                                  {
                                                      EnableExceptionLogging = true,
                                                      IsDebug = env.IsDevelopment()                       || Debugger.IsAttached,
                                                      UseApiProblemDetailsException = env.IsDevelopment() || Debugger.IsAttached,
                                                      ShowApiVersion = true,
                                                      ShowStatusCode = true,
                                                      LogRequestDataOnException = true,
                                                      EnableResponseLogging = true,
                                                      ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                                                      ApiVersion = "1.0"
                                                  });

What am I missing or what did I do wrong? Thanks much in advance!

ryanfaricy commented 4 years ago

Oops - I removed the following from my configuration definition and now it works as expected. Perhaps might be nice to persist the status code with this configuration, but in the meantime, maybe this will help someone else out one day. :) Thanks again!

UseApiProblemDetailsException = env.IsDevelopment() || Debugger.IsAttached,