-
-
Save dcomartin/9c99f11393f9d295d0dd0c409f5c55ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Net.Http.Json; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp1 | |
{ | |
public class ProblemDetailsHttpMessageHandler : DelegatingHandler | |
{ | |
public ProblemDetailsHttpMessageHandler() : base(new HttpClientHandler()) { } | |
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct) | |
{ | |
var response = await base.SendAsync(request, ct); | |
var mediaType = response.Content.Headers.ContentType?.MediaType; | |
if (mediaType != null && mediaType.Equals("application/problem+json", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
var problemDetails = await response.Content.ReadFromJsonAsync<ProblemDetails>(null, ct) ?? new ProblemDetails(); | |
throw new ProblemDetailsException(problemDetails, response); | |
} | |
return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment