Skip to content

Commit

Permalink
Handle non-json responses from instances #41 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
g3rv4 authored Jun 12, 2023
1 parent 6dcc1a0 commit fe7703b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.Json;
using GetMoarFediverse;
using GetMoarFediverse.Configuration;
using GetMoarFediverse.Responses;
using TurnerSoftware.RobotsExclusionTools;

var configPath = Environment.GetEnvironmentVariable("CONFIG_PATH");
Expand Down Expand Up @@ -109,20 +110,31 @@ await Parallel.ForEachAsync(Context.Configuration.Sites,
}

HttpResponseMessage? response = null;
string? json = null;
try
{
response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
json = await response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
Console.WriteLine($"Error fetching tag {tag} from {site}, status code: {response?.StatusCode}. Error: {e.Message}");
return;
}

var json = await response.Content.ReadAsStringAsync();

var data = JsonSerializer.Deserialize(json, CamelCaseJsonContext.Default.StatusResponseArray);
StatusResponse[]? data;
try
{
data = JsonSerializer.Deserialize(json, CamelCaseJsonContext.Default.StatusResponseArray);
}
catch (Exception e)
{
Console.WriteLine($"Error deserializing the response when pulling #{tag} posts from {site}. Error: {e.Message}");
Console.WriteLine($"Got the following response while I expected json content: {json}");
return;
}

if (data == null)
{
Console.WriteLine($"Error deserializing the response when pulling #{tag} posts from {site}");
Expand Down

0 comments on commit fe7703b

Please sign in to comment.