question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(C#) exceptions do not copy the response body into exceptions

See original GitHub issue

If I have a C# WebApi controller return a BadRequestErrorMessageResult (as an example, it’s valid to return a body with other status codes)

       [SwaggerResponse(HttpStatusCode.OK, "PutWidgetResponse", typeof(ApiResponse<WidgetModel>))]
        public async Task<IHttpActionResult> PutWidget(int id, [FromBody] WidgetModel widget)
        {
            // Validate
            if (id <= 0)
            {
                return BadRequest("id={0} is invalid", id);
            }
            // etc

The response body has the following JSON (from memory, it’s approximately this):

{ "message" : "id=0 is invalid"}

The generated C# proxy (I’ve removed a few irrelevant lines from this snippet) sets the response body to null:

            _httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
            HttpStatusCode _statusCode = _httpResponse.StatusCode;
            string _responseContent = null;
            if ((int)_statusCode != 200)
            {
                var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);

In my use case it’d be great to have the Response body get copied into the exception so we can mine it for more useful information.

Is it possible to get this changed so the response body is copied into the exception?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
koolraapcommented, Feb 10, 2016

TL;DR garbage in==garbage out.

Thanks, that clears it up. It was user error on my part and non-obvious Swashbuckle behaviour.

Initially I’d avoided decorating my controller methods with this… [SwaggerResponse(HttpStatusCode.Unauthorized, "", typeof(Exception))] …as it was generating object in Autorest. Turns out Swashbuckle doesn’t have XML documentation available for System.Exception so it doesn’t emit anything useful.

Changing to this… [SwaggerResponse(HttpStatusCode.Unauthorized, "", typeof(MyExceptionModel))] Autorest works as expected.

0reactions
brentonwcommented, Apr 17, 2016

@stankovski - so what happens when you can’t control the Swagger?

Why not just set the body and allow the caller of the generated code to access anything they need?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing response body from 422 in c# - Stack Overflow
The response body seems to be missing in the web exception so he never gets the reason for his error and can't correct...
Read more >
Exceptions and Error Handling, C++ FAQ - Standard C++
In C ++, exceptions are used to signal errors that cannot be handled locally, such as the failure to acquire a resource in...
Read more >
Exception Handling in ASP.NET Web API - Microsoft Learn
Describes ASP.NET Web API executes error and exception handling and provides examples for errors and exceptions.
Read more >
try...catch - JavaScript - MDN Web Docs - Mozilla
An optional identifier to hold the caught exception for the associated catch block. If the catch block does not utilize the exception's ......
Read more >
Request and response objects - Django documentation
Django uses request and response objects to pass state through the system. When a page is ... BadSignature exception if the signature is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found