Handle non-json responses from instances #41 (#42)

This commit is contained in:
Gervasio Marchand 2023-06-12 09:31:11 -03:00 committed by GitHub
parent 6dcc1a0327
commit fe7703bd9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
using System.Text.Json; using System.Text.Json;
using GetMoarFediverse; using GetMoarFediverse;
using GetMoarFediverse.Configuration; using GetMoarFediverse.Configuration;
using GetMoarFediverse.Responses;
using TurnerSoftware.RobotsExclusionTools; using TurnerSoftware.RobotsExclusionTools;
var configPath = Environment.GetEnvironmentVariable("CONFIG_PATH"); var configPath = Environment.GetEnvironmentVariable("CONFIG_PATH");
@ -109,10 +110,12 @@ await Parallel.ForEachAsync(sitesTags, new ParallelOptions{MaxDegreeOfParallelis
} }
HttpResponseMessage? response = null; HttpResponseMessage? response = null;
string? json = null;
try try
{ {
response = await client.GetAsync(url); response = await client.GetAsync(url);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
json = await response.Content.ReadAsStringAsync();
} }
catch (Exception e) catch (Exception e)
{ {
@ -120,9 +123,18 @@ await Parallel.ForEachAsync(sitesTags, new ParallelOptions{MaxDegreeOfParallelis
return; return;
} }
var json = await response.Content.ReadAsStringAsync(); 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;
}
var data = JsonSerializer.Deserialize(json, CamelCaseJsonContext.Default.StatusResponseArray);
if (data == null) if (data == null)
{ {
Console.WriteLine($"Error deserializing the response when pulling #{tag} posts from {site}"); Console.WriteLine($"Error deserializing the response when pulling #{tag} posts from {site}");