catch InvalidOperationException and add better logging

This commit is contained in:
Collin M. Barrett 2019-07-02 17:34:57 -05:00
parent 437eea994d
commit 747c6be1a9

View file

@ -8,6 +8,7 @@
using FilterLists.Agent.Features.Urls.Models.ValidationResults;
using FilterLists.Agent.Infrastructure.Clients;
using MediatR;
using Microsoft.Extensions.Logging;
namespace FilterLists.Agent.Features.Urls
{
@ -27,10 +28,12 @@ public class Handler : IRequestHandler<Command, List<UrlValidationResult>>
{
private const int MaxDegreeOfParallelism = 5;
private readonly HttpClient _httpClient;
private readonly ILogger<Handler> _logger;
public Handler(AgentHttpClient agentHttpClient)
public Handler(AgentHttpClient agentHttpClient, ILogger<Handler> logger)
{
_httpClient = agentHttpClient.Client;
_logger = logger;
}
public async Task<List<UrlValidationResult>> Handle(Command request, CancellationToken cancellationToken)
@ -68,16 +71,26 @@ private TransformBlock<Uri, UrlValidationResult> BuildValidator(CancellationToke
if (response.IsSuccessStatusCode)
return result;
result.SetBroken();
_logger.LogError(
$"Url validation for ({u.AbsoluteUri}) failed with status code: {response.StatusCode}.");
return result;
}
catch (HttpRequestException)
catch (HttpRequestException ex)
{
result.SetBroken();
_logger.LogError($"Url validation for ({u.AbsoluteUri}) failed.", ex);
return result;
}
catch (TaskCanceledException)
catch (TaskCanceledException ex)
{
result.SetBroken();
_logger.LogError($"Url validation for ({u.AbsoluteUri}) failed.", ex);
return result;
}
catch (InvalidOperationException ex)
{
result.SetBroken();
_logger.LogError($"Url validation for ({u.AbsoluteUri}) failed.", ex);
return result;
}
},
@ -91,14 +104,25 @@ private async Task<bool> IsHttpsSupported(Uri url, CancellationToken cancellatio
try
{
var response = await _httpClient.GetAsync(httpsUrl, cancellationToken);
if (response.IsSuccessStatusCode)
return true;
_logger.LogError(
$"IsHttpsSupported({httpsUrl.AbsoluteUri}) failed with status code: {response.StatusCode}.");
return response.IsSuccessStatusCode;
}
catch (HttpRequestException)
catch (HttpRequestException ex)
{
_logger.LogError($"IsHttpsSupported({httpsUrl.AbsoluteUri}) failed.", ex);
return false;
}
catch (TaskCanceledException)
catch (TaskCanceledException ex)
{
_logger.LogError($"IsHttpsSupported({httpsUrl.AbsoluteUri}) failed.", ex);
return false;
}
catch (InvalidOperationException ex)
{
_logger.LogError($"IsHttpsSupported({httpsUrl.AbsoluteUri}) failed.", ex);
return false;
}
}