From fecf8fb1e3124304f8c9c8a9e07fc8051845964d Mon Sep 17 00:00:00 2001 From: Louis Fischer Date: Mon, 26 Jun 2017 21:35:16 -0500 Subject: [PATCH] Added EnsureResponseStatus with optional throw option --- src/Cake.Http/CakeHttpClientHandler.cs | 15 ++++++++++++++- src/Cake.Http/HttpSettings.cs | 6 ++++++ src/Cake.Http/HttpSettingsExtensions.cs | 4 +++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Cake.Http/CakeHttpClientHandler.cs b/src/Cake.Http/CakeHttpClientHandler.cs index fb75855..0c1196b 100644 --- a/src/Cake.Http/CakeHttpClientHandler.cs +++ b/src/Cake.Http/CakeHttpClientHandler.cs @@ -65,6 +65,7 @@ protected override async Task 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(); @@ -75,8 +76,20 @@ protected override async Task 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; } diff --git a/src/Cake.Http/HttpSettings.cs b/src/Cake.Http/HttpSettings.cs index 2e5a963..39da364 100644 --- a/src/Cake.Http/HttpSettings.cs +++ b/src/Cake.Http/HttpSettings.cs @@ -44,5 +44,11 @@ public HttpSettings() /// Gets or Sets whether to throw an exception if the returned response is not a Successful Status Code /// public bool EnsureSuccessStatusCode { get; set; } + + /// + /// Gets or Sets where an exception is thrown on non success code. + /// This is used in conjunction with EnsureSuccessStatusCode. + /// + public bool ThrowExceptionOnNonSuccessStatusCode { get; set; } } } \ No newline at end of file diff --git a/src/Cake.Http/HttpSettingsExtensions.cs b/src/Cake.Http/HttpSettingsExtensions.cs index 19a9a26..d386b41 100644 --- a/src/Cake.Http/HttpSettingsExtensions.cs +++ b/src/Cake.Http/HttpSettingsExtensions.cs @@ -269,13 +269,15 @@ public static HttpSettings SetJsonRequestBody(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. /// /// The settings. + /// Determines if an exception is thrown on non-success code. /// The same instance so that multiple calls can be chained. - 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; }