Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,14 @@ public async void SetsTheApiExceptionStatusCode(HttpStatusCode statusCode) {
var client = new HttpClient(mockHandler.Object);
mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.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
{
Expand All @@ -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"));
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IParsable>? errorFactory;
if(errorMapping == null ||
!errorMapping.TryGetValue(statusCodeAsString, out errorFactory) &&
Expand All @@ -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);
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.Kiota.Http.HttpClientLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://microsoft.github.io/kiota/</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.0.1</VersionPrefix>
<VersionPrefix>1.0.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
Expand All @@ -30,12 +30,13 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.1.0" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.1.1" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="[6.0,8.0)" />
<PackageReference Include="System.Text.Json" Version="[6.0,8.0)" />
<ProjectReference Include="..\Kiota.Generated\KiotaGenerated.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Expand Down