mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(archival): ✨ catch and log HttpRequestException
This commit is contained in:
parent
2f187cd9da
commit
6c9ae48819
1 changed files with 14 additions and 3 deletions
|
|
@ -4,6 +4,7 @@
|
|||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FilterLists.Archival.Infrastructure.Clients
|
||||
{
|
||||
|
|
@ -16,18 +17,28 @@ internal sealed class HttpContentClient : IHttpContentClient
|
|||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ICollection<HttpResponseMessage> _httpResponseMessages = new List<HttpResponseMessage>();
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public HttpContentClient(HttpClient httpClient)
|
||||
public HttpContentClient(HttpClient httpClient, ILogger<HttpContentClient> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<Stream> GetContentAsync(Uri url, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
||||
_httpResponseMessages.Add(response);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStreamAsync();
|
||||
try
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStreamAsync();
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get content from {Url}", url);
|
||||
return Stream.Null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
|
|
|||
Loading…
Reference in a new issue