autorest icon indicating copy to clipboard operation
autorest copied to clipboard

(C#) If JSON deserialization fails, the exception should capture the response body

Open bitsforthought opened this issue 10 years ago • 2 comments

The generated C# code for an operation currently includes a block like this for deserializing the response:

// Deserialize Response
if ((int)statusCode == 200)
{
    try
    {
        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
        result.Body = JsonConvert.DeserializeObject<AdminKeyResult>(responseContent, this.Client.DeserializationSettings);
    }
    catch (JsonException ex)
    {
        throw new RestException("Unable to deserialize the response.", ex);
    }
}
if (shouldTrace)
{
    ServiceClientTracing.Exit(invocationId, result);
}
return result;

Note that if RestException is thrown, the response isn't traced. The value of responseContent should be included in the RestException to help users troubleshoot deserialization failures.

bitsforthought avatar Dec 07 '15 20:12 bitsforthought

Fixed as part of https://github.com/Azure/autorest/pull/625

stankovski avatar Jan 22 '16 17:01 stankovski

Thanks!

bitsforthought avatar Jan 23 '16 00:01 bitsforthought