Skip to content

Commit

Permalink
Fixed exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Feb 23, 2024
1 parent fb4f339 commit bf5fac3
Showing 1 changed file with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Blazorise;

using BUTR.Site.NexusMods.ServerClient;

using System;
using System.Net;
using System.Net.Http;
Expand All @@ -18,21 +20,40 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
if (await _tokenContainer.GetTokenAsync(ct) is { Type: "demo" })
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") };

var response = await base.SendAsync(request, ct);

if (response.StatusCode == HttpStatusCode.Unauthorized)
try
{
await _tokenContainer.SetTokenAsync(null, ct);
await _notificationService.Error("Authentication failure! Please log in again!", "Error!");
var response = await base.SendAsync(request, ct);

if (response.StatusCode == HttpStatusCode.Unauthorized)
{
await _tokenContainer.SetTokenAsync(null, ct);
await _notificationService.Error("Authentication failure! Please log in again!", "Error!");
}

// Cloudflare timeout
if ((int) response.StatusCode == 522)
{
await _notificationService.Error("Backend is down! Notify about the issue on GitHub https://github.com/BUTR/BUTR.Site.NexusMods", "Error!");
}

return response;
}

// Cloudflare timeout
if ((int) response.StatusCode == 522)
catch (ApiException e)
{
Console.WriteLine(522);
await _notificationService.Error("Backend is down! Notify about the issue on GitHub https://github.com/BUTR/BUTR.Site.NexusMods", "Error!");
// Cloudflare timeout
if ( e.StatusCode == (int)HttpStatusCode.Unauthorized)
{
await _tokenContainer.SetTokenAsync(null, ct);
await _notificationService.Error("Authentication failure! Please log in again!", "Error!");
}

// Cloudflare timeout
if (e.StatusCode == 522)
{
await _notificationService.Error("Backend is down! Notify about the issue on GitHub https://github.com/BUTR/BUTR.Site.NexusMods", "Error!");
}

throw;
}

return response;
}
}

0 comments on commit bf5fac3

Please sign in to comment.