diff --git a/src/SendGrid/SendGridClient.cs b/src/SendGrid/SendGridClient.cs index 71d656791..c4cdd9110 100644 --- a/src/SendGrid/SendGridClient.cs +++ b/src/SendGrid/SendGridClient.cs @@ -20,9 +20,10 @@ public class SendGridClient : BaseClient /// A dictionary of request headers. /// API version, override AddVersion to customize. /// Path to endpoint (e.g. /path/to/endpoint). + /// Indicates whether HTTP error responses should be raised as exceptions. Default is false. /// Interface to the Twilio SendGrid REST API. public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dictionary requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false) - : base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath)) + : base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException)) { } @@ -35,9 +36,10 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic /// A dictionary of request headers. /// API version, override AddVersion to customize. /// Path to endpoint (e.g. /path/to/endpoint). + /// Indicates whether HTTP error responses should be raised as exceptions. Default is false. /// Interface to the Twilio SendGrid REST API. public SendGridClient(HttpClient httpClient, string apiKey, string host = null, Dictionary requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false) - : base(httpClient, buildOptions(apiKey, host, requestHeaders, version, urlPath)) + : base(httpClient, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException)) { } @@ -51,7 +53,7 @@ public SendGridClient(HttpClient httpClient, string apiKey, string host = null, /// Path to endpoint (e.g. /path/to/endpoint). /// Interface to the Twilio SendGrid REST API. public SendGridClient(string apiKey, string host = null, Dictionary requestHeaders = null, string version = null, string urlPath = null) - : base(buildOptions(apiKey, host, requestHeaders, version, urlPath)) + : base(buildOptions(apiKey, host, requestHeaders, version, urlPath, false)) { } @@ -76,7 +78,7 @@ public SendGridClient(HttpClient httpClient, SendGridClientOptions options) { } - private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary requestHeaders, string version, string urlPath) + private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary requestHeaders, string version, string urlPath, bool httpErrorAsException) { return new SendGridClientOptions { @@ -85,6 +87,7 @@ private static SendGridClientOptions buildOptions(string apiKey, string host, Di RequestHeaders = requestHeaders ?? DefaultOptions.RequestHeaders, Version = version ?? DefaultOptions.Version, UrlPath = urlPath ?? DefaultOptions.UrlPath, + HttpErrorAsException = httpErrorAsException }; } } diff --git a/tests/SendGrid.Tests/Integration.cs b/tests/SendGrid.Tests/Integration.cs index 384d7d372..f5db6f4bc 100644 --- a/tests/SendGrid.Tests/Integration.cs +++ b/tests/SendGrid.Tests/Integration.cs @@ -6121,7 +6121,18 @@ public async Task TestRetryBehaviourSucceedsOnSecondAttempt() } [Fact] - public async void TestHttpErrorAsException() + public async void TestHttpErrorAsExceptionWhenSetInOptions() + { + await TestHttpErrorAsException((client, apiKey) => new SendGridClient(client, new SendGridClientOptions {ApiKey = apiKey, HttpErrorAsException = true})); + } + + [Fact] + public async void TestHttpErrorAsExceptionWhenSetAsParameter() + { + await TestHttpErrorAsException((client, apiKey) => new SendGridClient(client, apiKey, httpErrorAsException: true)); + } + + private async Task TestHttpErrorAsException(Func createClientFunc) { var responseObject = new { @@ -6138,7 +6149,7 @@ public async void TestHttpErrorAsException() }); var mockHandler = new FixedStatusAndMessageHttpMessageHandler(HttpStatusCode.ServiceUnavailable, responseMessage); var mockClient = new HttpClient(mockHandler); - var client = new SendGridClient(mockClient, new SendGridClientOptions { ApiKey = fixture.apiKey, HttpErrorAsException = true }); + var client = createClientFunc(mockClient, fixture.apiKey); try {