From 92bc3d5aa8bb837cd76d1ac3d6561373dd0c794a Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 2 Nov 2023 15:19:56 +0100 Subject: [PATCH] Catch HttpRequestException in LibraryConnector --- Tranga/LibraryConnectors/LibraryConnector.cs | 34 +++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/Tranga/LibraryConnectors/LibraryConnector.cs b/Tranga/LibraryConnectors/LibraryConnector.cs index 3c975e7..fb5d177 100644 --- a/Tranga/LibraryConnectors/LibraryConnector.cs +++ b/Tranga/LibraryConnectors/LibraryConnector.cs @@ -41,15 +41,33 @@ public abstract class LibraryConnector : GlobalBase Method = HttpMethod.Get, RequestUri = new Uri(url) }; - HttpResponseMessage response = client.Send(requestMessage); - logger?.WriteLine("LibraryManager.NetClient", $"GET {url} -> {(int)response.StatusCode}: {response.ReasonPhrase}"); - - if(response.StatusCode is HttpStatusCode.Unauthorized && response.RequestMessage!.RequestUri!.AbsoluteUri != url) - return MakeRequest(response.RequestMessage!.RequestUri!.AbsoluteUri, authScheme, auth, logger); - else if (response.IsSuccessStatusCode) - return response.Content.ReadAsStream(); - else + try + { + + HttpResponseMessage response = client.Send(requestMessage); + logger?.WriteLine("LibraryManager.NetClient", + $"GET {url} -> {(int)response.StatusCode}: {response.ReasonPhrase}"); + + if (response.StatusCode is HttpStatusCode.Unauthorized && + response.RequestMessage!.RequestUri!.AbsoluteUri != url) + return MakeRequest(response.RequestMessage!.RequestUri!.AbsoluteUri, authScheme, auth, logger); + else if (response.IsSuccessStatusCode) + return response.Content.ReadAsStream(); + else + return Stream.Null; + } + catch (Exception e) + { + switch (e) + { + case HttpRequestException: + logger?.WriteLine("LibraryManager.NetClient", $"Failed to make Request:\n\r{e}"); + break; + default: + throw; + } return Stream.Null; + } } public static bool MakePost(string url, string authScheme, string auth, Logger? logger)