2
0

Catch HttpRequestException in LibraryConnector

This commit is contained in:
glax 2023-11-02 15:19:56 +01:00
parent 49ab8928b1
commit 92bc3d5aa8

View File

@ -41,15 +41,33 @@ public abstract class LibraryConnector : GlobalBase
Method = HttpMethod.Get, Method = HttpMethod.Get,
RequestUri = new Uri(url) RequestUri = new Uri(url)
}; };
HttpResponseMessage response = client.Send(requestMessage); try
logger?.WriteLine("LibraryManager.NetClient", $"GET {url} -> {(int)response.StatusCode}: {response.ReasonPhrase}"); {
if(response.StatusCode is HttpStatusCode.Unauthorized && response.RequestMessage!.RequestUri!.AbsoluteUri != url) HttpResponseMessage response = client.Send(requestMessage);
return MakeRequest(response.RequestMessage!.RequestUri!.AbsoluteUri, authScheme, auth, logger); logger?.WriteLine("LibraryManager.NetClient",
else if (response.IsSuccessStatusCode) $"GET {url} -> {(int)response.StatusCode}: {response.ReasonPhrase}");
return response.Content.ReadAsStream();
else 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; return Stream.Null;
}
} }
public static bool MakePost(string url, string authScheme, string auth, Logger? logger) public static bool MakePost(string url, string authScheme, string auth, Logger? logger)