You can add a MessageHandler
to do this.
MessageHandlers
run first and last in the pipeline, allowing you to process raw incoming request and raw outgoing response.
For example:
public class ErrorHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
if (!response.IsSuccessStatusCode)
{
Debug.WriteLine("something happened! - " + response.ReasonPhrase);
}
return response;
}
}
And then register in your GlobalConfiguration
config.MessageHandlers.Add(new ErrorHandler());
This basically inspects the outgoing response and checks if the status code is 2xx. If not you can do something with it - log, or perhaps reset the content of the response to hide whatever you wanna hide.