Skip to content

Commit

Permalink
Merge pull request #26 from cake-contrib/feature/22/ensure-success-co…
Browse files Browse the repository at this point in the history
…de-with-response

Added EnsureResponseStatus with optional throw option
  • Loading branch information
louisfischer committed Jun 27, 2017
2 parents 2a03a7b + fecf8fb commit 920d661
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/Cake.Http/CakeHttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
byte[] responseMessage = null;
if (response.IsSuccessStatusCode && response.Content != null)
responseMessage = await response.Content.ReadAsByteArrayAsync();

else if (response.Content != null)
{
var tempMessage = await response.Content.ReadAsStringAsync();
Expand All @@ -75,8 +76,20 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

await LogHttpEvent(id, HttpEventType.Response, requestInfo, responseMessage);

// Determines whether to ensure Status code
if (_Settings.EnsureSuccessStatusCode)
response.EnsureSuccessStatusCode();
{
try
{
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
// Only throw exception on when Non-Success Status Code
if (_Settings.ThrowExceptionOnNonSuccessStatusCode)
throw ex;
}
}

return response;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Cake.Http/HttpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ public HttpSettings()
/// Gets or Sets whether to throw an exception if the returned response is not a Successful Status Code
/// </summary>
public bool EnsureSuccessStatusCode { get; set; }

/// <summary>
/// Gets or Sets where an exception is thrown on non success code.
/// This is used in conjunction with EnsureSuccessStatusCode.
/// </summary>
public bool ThrowExceptionOnNonSuccessStatusCode { get; set; }
}
}
4 changes: 3 additions & 1 deletion src/Cake.Http/HttpSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,15 @@ public static HttpSettings SetJsonRequestBody<T>(this HttpSettings settings, T d
/// Sets the EnsureSuccessStatusCode to true. This makes the httpclient throw an error if it does not return a 200 range status.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="throwExceptionOnNonSuccessStatusCode">Determines if an exception is thrown on non-success code.</param>
/// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
public static HttpSettings EnsureSuccessStatusCode(this HttpSettings settings)
public static HttpSettings EnsureSuccessStatusCode(this HttpSettings settings, bool throwExceptionOnNonSuccessStatusCode = true)
{
if (settings == null)
throw new ArgumentNullException(nameof(settings));

settings.EnsureSuccessStatusCode = true;
settings.ThrowExceptionOnNonSuccessStatusCode = throwExceptionOnNonSuccessStatusCode;

return settings;
}
Expand Down

0 comments on commit 920d661

Please sign in to comment.