HTTP responses don't have information from body on non-success status
See original GitHub issueWhen the response of a call doesn’t have a success status code, the call throws an HttpRequestException
with no context for what went wrong beyond the actual status code. EnsureSuccessStatusCode
is called and the content of the response is lost.
In some (most?) OpenAPI schemas, the shape of the response for non-success status codes is also documented and a schema is provided for each media type, just as it is for success codes. SwaggerProvider should see if any are present, and if so generate custom exceptions which can be thrown containing the deserialised body when such statuses are returned.
This will greatly help with diagnosing errors that come from a service, when eg. they provide further info than just “bad request” (what about my request was bad?).
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (7 by maintainers)
Top GitHub Comments
I understand this doesn’t help with getting the error as defined in the schema, but if you want a workaround to at least get the body when it throws, you can write some middleware like this:
and use it like:
(Or you could implement an
ErrorLoggingHandler
which accepts a delegate on construction if you just want to log.)Yeah, probably a preferred and more functional way would be to have a union type returned like
but alas, provided union types can’t be done 😢 (https://github.com/fsharp/fslang-suggestions/issues/154).
You could get around the issues above with a tweak to the multiple exception route though - for the issue of trying to catch them all, they could all inherit from
OpenApiException
as it’s implemented now. Then clients who don’t care about the response body or distinguishing between different codes can just catchOpenApiException
, and clients who only care for a subset of them can do eg.To solve the discoverability issue, could these custom exception types all be nested in a hierarchy (since we can now open static classes)?
(writing in C# as nested types aren’t in F#, but I assume it’s still possible to generate them via type providers?)