mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
catch InvalidOperationException and add better logging
This commit is contained in:
parent
437eea994d
commit
747c6be1a9
1 changed files with 29 additions and 5 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue