From fe7703bd9f772ae6d3fd5c2e276eccf7621a45af Mon Sep 17 00:00:00 2001 From: Gervasio Marchand Date: Mon, 12 Jun 2023 09:31:11 -0300 Subject: [PATCH] Handle non-json responses from instances #41 (#42) --- src/Program.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Program.cs b/src/Program.cs index 677b01b..d6a7562 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -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"); @@ -109,10 +110,12 @@ 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) { @@ -120,9 +123,18 @@ await Parallel.ForEachAsync(Context.Configuration.Sites, 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}");