Skip to content

Commit

Permalink
Error handling for exceptions being thrown while probing a URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackatrons committed Feb 2, 2022
1 parent c5850eb commit 2f7f200
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions DiscordBot/Filters/UrlCheckFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,24 @@ public IAsyncEnumerable<SearchResult> Filter(IAsyncEnumerable<SearchResult> inpu

using var client = _httpClientFactory.CreateClient();
using var request = new HttpRequestMessage(HttpMethod.Head, uri);
using var response = await client.SendAsync(request);

// we may have followed some redirects in which case the end url is different to the original url
// provide the redirected url as discord doesn't seem to follow redirects
var responseUrl = response.RequestMessage?.RequestUri?.ToString();
try
{
using var response = await client.SendAsync(request);

// we may have followed some redirects in which case the end url is different to the original url
// provide the redirected url as discord doesn't seem to follow redirects
var responseUrl = response.RequestMessage?.RequestUri?.ToString();

if (!response.IsSuccessStatusCode)
_logger.LogDebug("Excluding result {url} as the url returns a {code} response.", url, response.StatusCode);
if (!response.IsSuccessStatusCode)
_logger.LogDebug("Excluding result {url} as the url returns a {code} response.", url, response.StatusCode);

return (response.IsSuccessStatusCode, response.Headers.ETag?.Tag, responseUrl);
return (response.IsSuccessStatusCode, response.Headers.ETag?.Tag, responseUrl);
}
catch (HttpRequestException ex)
{
_logger.LogWarning("Error while testing url {url}. Error: {error}", url, ex);
return (false, null, null);
}
}
}

0 comments on commit 2f7f200

Please sign in to comment.