diff --git a/CHANGELOG.md b/CHANGELOG.md index dbb5c8e..7c89b4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +## [1.0.2] - 2023-04-06 + +### Changed + +- Includes Response headers in APIException for failed requests. + ## [1.0.1] - 2023-03-10 ### Changed diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs index 0b3a8a9..254e39f 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs @@ -324,9 +324,14 @@ public async void SetsTheApiExceptionStatusCode(HttpStatusCode statusCode) { var client = new HttpClient(mockHandler.Object); mockHandler.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) - .ReturnsAsync(new HttpResponseMessage { - StatusCode = statusCode, - }); + .ReturnsAsync(() => { + var responseMessage = new HttpResponseMessage + { + StatusCode = statusCode + }; + responseMessage.Headers.Add("request-id", "guid-value"); + return responseMessage; + }); var adapter = new HttpClientRequestAdapter(_authenticationProvider, httpClient: client); var requestInfo = new RequestInformation { @@ -338,6 +343,7 @@ public async void SetsTheApiExceptionStatusCode(HttpStatusCode statusCode) { Assert.Fail("Expected an ApiException to be thrown"); } catch (ApiException e) { Assert.Equal((int)statusCode, e.ResponseStatusCode); + Assert.True(e.ResponseHeaders.ContainsKey("request-id")); } } } diff --git a/src/HttpClientRequestAdapter.cs b/src/HttpClientRequestAdapter.cs index cfb30b7..51ac75a 100644 --- a/src/HttpClientRequestAdapter.cs +++ b/src/HttpClientRequestAdapter.cs @@ -343,6 +343,7 @@ private async Task ThrowIfFailedResponse(HttpResponseMessage response, Dictionar var statusCodeAsInt = (int)response.StatusCode; var statusCodeAsString = statusCodeAsInt.ToString(); + var responseHeadersDictionary = response.Headers.ToDictionary(x => x.Key,y => y.Value,StringComparer.OrdinalIgnoreCase); ParsableFactory? errorFactory; if(errorMapping == null || !errorMapping.TryGetValue(statusCodeAsString, out errorFactory) && @@ -352,6 +353,7 @@ private async Task ThrowIfFailedResponse(HttpResponseMessage response, Dictionar activityForAttributes?.SetTag(ErrorMappingFoundAttributeName, false); throw new ApiException($"The server returned an unexpected status code and no error factory is registered for this code: {statusCodeAsString}") { ResponseStatusCode = statusCodeAsInt, + ResponseHeaders = responseHeadersDictionary }; } activityForAttributes?.SetTag(ErrorMappingFoundAttributeName, true); @@ -365,9 +367,14 @@ private async Task ThrowIfFailedResponse(HttpResponseMessage response, Dictionar if(result is not Exception ex) throw new ApiException($"The server returned an unexpected status code and the error registered for this code failed to deserialize: {statusCodeAsString}") { ResponseStatusCode = statusCodeAsInt, + ResponseHeaders = responseHeadersDictionary }; if(result is ApiException apiEx) + { apiEx.ResponseStatusCode = statusCodeAsInt; + apiEx.ResponseHeaders = responseHeadersDictionary; + } + throw ex; } private static IResponseHandler? GetResponseHandler(RequestInformation requestInfo) diff --git a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj index 2744846..c8fd138 100644 --- a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj +++ b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj @@ -13,7 +13,7 @@ https://microsoft.github.io/kiota/ true true - 1.0.1 + 1.0.2 true @@ -30,12 +30,13 @@ README.md latest enable + true true - +