Differences in behavior
See original GitHub issueHello again:
I have added your Middleware to an API, but within the API, developers have used the following methods when dealing with 400 errors:
BadRequest()
BadRequest("My message")
BadRequest(new Exception("Regular exception"))
BadRequest(new ArgumentException("The argument is invalid", nameof(myParameter)))
BadRequest(new ArgumentNullException(nameof(statusCode), "The argument is null"))
I have a console app that I’m using to test what I get back from our controllers using your Middleware. I am getting different things back for the scenarios above, but I’m not sure if the issue is in the Middleware or in how I’m processing the response. For example, the first style, just returning BadRequest()
with no message gives me what I expect: a ProblemDetails
object in JSON. However, using BadRequest("My message")
returns only the value “My message” in the response content, so any of my code that expects to deserialize a ProblemDetails
object is failing.
BadRequest(new Exception("Regular exception"))
gives back response content matching the Exception:
{"targetSite":null,"stackTrace":null,"message":"Regular exception","data":{},"innerException":null,"helpLink":null,"source":null,"hResult":-2146233088}
When I deserialize this to ProblemDetails
, all the properties of the object are Null, except for the Extensions
property which now has eight entries, representing the eight items in the content string above. The other BadRequest
items behave similarly, but with added Extension
entries for the parameter name.
I would like these situations to behave the same way, e.g., Any BadRequest
comes back as deserializable response content, (including the message in the BadRequest
) without having to overhaul my APIs. Is that possible? Should I be using exceptions inside my BadRequests, or just messages? Do I need a custom ProblemDetails
or should I be able to use the default? Are there any options I need to set?
Ultimately, I would like to be able to use your middleware without having to alter my APIs, like changing the BadRequest("My message")
style returns to BadRequest(new ProblemDetails())
calls.
I appreciate any help you can offer.
Issue Analytics
- State:
- Created 3 years ago
- Comments:27 (15 by maintainers)
Top GitHub Comments
I figured it out. The call to
UseProblemDetails
has to come after the call toUseDeveloperExceptionPage
. This now works, e.g., I get a properProblemDetails
instance:I asked the question above in [#123]