From 37266ea0952b024b30b328ebab9bb7f337a9db77 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 30 Oct 2023 13:48:25 +0100 Subject: [PATCH] https://github.com/C9Glax/tranga-website/issues/19 Add exception handling if host doesnt exist --- Tranga/LibraryConnectors/Kavita.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Tranga/LibraryConnectors/Kavita.cs b/Tranga/LibraryConnectors/Kavita.cs index a6442f2..106c90e 100644 --- a/Tranga/LibraryConnectors/Kavita.cs +++ b/Tranga/LibraryConnectors/Kavita.cs @@ -37,12 +37,19 @@ public class Kavita : LibraryConnector RequestUri = new Uri($"{baseUrl}/api/Account/login"), Content = new StringContent($"{{\"username\":\"{username}\",\"password\":\"{password}\"}}", System.Text.Encoding.UTF8, "application/json") }; - - HttpResponseMessage response = client.Send(requestMessage); - JsonObject? result = JsonSerializer.Deserialize(response.Content.ReadAsStream()); - if (result is not null) - return result["token"]!.GetValue(); - else throw new Exception("Did not receive token."); + try + { + HttpResponseMessage response = client.Send(requestMessage); + JsonObject? result = JsonSerializer.Deserialize(response.Content.ReadAsStream()); + if (result is not null) + return result["token"]!.GetValue(); + } + catch (HttpRequestException e) + { + Console.WriteLine($"Unable to retrieve token:\n\r{e}"); + } + Console.WriteLine("Kavita: Did not receive token."); + throw new Exception("Kavita: Did not receive token."); } public override void UpdateLibrary()