Skip to content

Commit

Permalink
fix: ensure we can properly read response content with incorrectly de…
Browse files Browse the repository at this point in the history
…fined serializer - e.g. for Unauthorized response
  • Loading branch information
Per Kops committed Jun 28, 2024
1 parent 19d9340 commit a824dc4
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/Atc.Rest.Client/Builder/MessageResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public IMessageResponseBuilder AddSuccessResponse<TResponseContent>(
HttpStatusCode statusCode)
=> AddTypedResponse<TResponseContent>(statusCode, isSuccess: true);

[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "OK.")]
public async Task<TResult> BuildResponseAsync<TResult>(
Func<EndpointResponse, TResult> factory,
CancellationToken cancellationToken)
Expand All @@ -57,13 +58,28 @@ public async Task<TResult> BuildResponseAsync<TResult>(
{
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

return factory(
new EndpointResponse(
IsSuccessStatus(response),
response.StatusCode,
content,
GetSerializer(response.StatusCode)?.Invoke(content),
GetHeaders(response)));
object? contentResponse = content;
var contentSerializerDelegate = GetSerializer(response.StatusCode);
if (contentSerializerDelegate is not null)
{
try
{
contentResponse = contentSerializerDelegate.Invoke(content);
}
catch
{
// Swallow
}
}

var endpointResponse = new EndpointResponse(
IsSuccessStatus(response),
response.StatusCode,
content,
contentResponse,
GetHeaders(response));

return factory(endpointResponse);
}

var contentObject = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
Expand Down

0 comments on commit a824dc4

Please sign in to comment.