Skip to content

Commit

Permalink
Fix crash when uri cannot be reached
Browse files Browse the repository at this point in the history
caused by OpenLibrary being down at the moment
  • Loading branch information
DSPaul committed Oct 11, 2024
1 parent 85768e9 commit 1b174b4
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Services/IOService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ public static async Task<byte[]> DownloadFileAsync(string uri)
using HttpClient client = new();

if (!Uri.TryCreate(uri, UriKind.Absolute, out Uri? _)) throw new InvalidOperationException("URI is invalid.");

return await client.GetByteArrayAsync(uri);
try
{
return await client.GetByteArrayAsync(uri);
}
catch (Exception ex)
{
Logger.Error($"Failed to fetch data at {uri}", ex);
return Array.Empty<byte>();
}
}
public static async Task<JObject?> GetJsonAsync(string uri)
{
Expand All @@ -44,12 +51,18 @@ public static async Task<byte[]> DownloadFileAsync(string uri)
JObject? json = null;

if (!Uri.TryCreate(uri, UriKind.Absolute, out Uri? _)) throw new InvalidOperationException("URI is invalid.");

HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
try
{
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
json = JObject.Parse(data.Result);
}
}
catch (Exception ex)
{
var data = response.Content.ReadAsStringAsync();
json = JObject.Parse(data.Result);
Logger.Error($"Failed to fetch data at {uri}", ex);
}
return json;
}
Expand Down

0 comments on commit 1b174b4

Please sign in to comment.