Skip to content

Instantly share code, notes, and snippets.

@crozone
Last active August 26, 2021 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crozone/782c03a63b368b6b3f1ac1bd5c27d889 to your computer and use it in GitHub Desktop.
Save crozone/782c03a63b368b6b3f1ac1bd5c27d889 to your computer and use it in GitHub Desktop.
EnsureSuccessStatusCodeWithStatus (HttpResponseMessageExtensions)
// Copyright 2021 Ryan Crosby
// This code is licensed under the MIT license
/// <summary>
/// Variations of the EnsureSuccessStatusCode() extension method for capturing and retreiving the HTTP Status Code and Response Body.
/// </summary>
public static class HttpResponseMessageExtensions
{
public const string StatusCodeKey = "StatusCode";
public const string ResponseBodyKey = "ResponseBody";
public const string ResponseBodyExceptionKey = "ResponseBodyException";
/// <summary>
/// Throws an exception if the System.Net.Http.HttpResponseMessage.IsSuccessStatusCode
/// property for the HTTP response is false.
/// The HTTP status code and response body will be stored in the exception data dictionary.
/// </summary>
/// <param name="httpResponseMessage"></param>
/// <returns>The HTTP response message if the call is successful.</returns>
/// <exception cref="System.Net.Http.HttpRequestException">The HTTP response is unsuccessful.
/// The HTTP status code will be attached to the exception data dictionary.
/// The HTTP response body will be attached to the exception data dictionary if it can be successfully read.
/// The HTTP response body exception will be attached to the exception data dictionary if the HTTP response body cannot be successfully read.
/// </exception>
public static async Task<HttpResponseMessage> EnsureSuccessStatusCodeWithStatusAndBody(this HttpResponseMessage httpResponseMessage)
{
try
{
return httpResponseMessage.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
ex.Data[StatusCodeKey] = httpResponseMessage.StatusCode;
try
{
ex.Data[ResponseBodyKey] = await httpResponseMessage.Content.ReadAsStringAsync();
}
catch (Exception innerEx)
{
ex.Data[ResponseBodyExceptionKey] = innerEx;
}
throw;
}
}
/// <summary>
/// Throws an exception if the System.Net.Http.HttpResponseMessage.IsSuccessStatusCode
/// property for the HTTP response is false.
/// The HTTP status code will be stored in the exception data dictionary.
/// </summary>
/// <param name="httpResponseMessage"></param>
/// <returns>The HTTP response message if the call is successful.</returns>
/// <exception cref="System.Net.Http.HttpRequestException">The HTTP response is unsuccessful.
/// The HTTP status code will be attached to the exception data dictionary.
/// </exception>
public static HttpResponseMessage EnsureSuccessStatusCodeWithStatus(this HttpResponseMessage httpResponseMessage)
{
try
{
return httpResponseMessage.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
ex.Data[StatusCodeKey] = httpResponseMessage.StatusCode;
throw;
}
}
/// <summary>
/// Retrieves the HTTP Status Code from a HttpRequestException thrown by EnsureSuccessStatusCodeWithStatus() or EnsureSuccessStatusCodeWithStatusAndBody().
/// </summary>
/// <param name="httpRequestException"></param>
/// <returns>The HttpStatusCode. Returns null if the status code was not present in the HttpRequestException data dictionary.</returns>
public static HttpStatusCode? GetHttpStatusCode(this HttpRequestException httpRequestException)
{
return httpRequestException.Data[StatusCodeKey] as HttpStatusCode?;
}
/// <summary>
/// Retrieves the HTTP Response Body from a HttpRequestException thrown by EnsureSuccessStatusCodeWithStatusAndBody().
/// </summary>
/// <param name="httpRequestException"></param>
/// <param name="suppressException">If true, this method will rethrow any exception encountered while reading the response body in the original EnsureSuccessStatusCodeWithStatusAndBody() call.</param>
/// <returns>The response body. Returns null if the response body was not present in the HttpRequestException data dictionary.</returns>
/// <exception cref="Exception">If throwException is true, rethrows any exception that was caught while reading the response body in the original EnsureSuccessStatusCodeWithStatusAndBody() call.</exception>
public static string GetResponseBody(this HttpRequestException httpRequestException, bool throwException = false)
{
var exception = httpRequestException.Data[ResponseBodyExceptionKey] as Exception;
if (throwException && exception != null)
{
throw exception;
}
else
{
return httpRequestException.Data[ResponseBodyKey] as string;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment