From 38d7fe25fd0b0df37d152c12615ebf6c5905b58d Mon Sep 17 00:00:00 2001 From: dangershony Date: Fri, 10 Nov 2023 16:34:13 +0000 Subject: [PATCH] handle network errors in the backgorund --- .../Client/Shared/NotificationComponent.razor | 4 +++- src/Angor/Shared/Services/INetworkService.cs | 2 ++ .../Services/NetworkService.cs | 23 ++++++++++++++----- 3 files changed, 22 insertions(+), 7 deletions(-) rename src/Angor/{Client => Shared}/Services/NetworkService.cs (86%) diff --git a/src/Angor/Client/Shared/NotificationComponent.razor b/src/Angor/Client/Shared/NotificationComponent.razor index 89c1c3b2..9bee14a1 100644 --- a/src/Angor/Client/Shared/NotificationComponent.razor +++ b/src/Angor/Client/Shared/NotificationComponent.razor @@ -1,7 +1,8 @@ @using Angor.Shared.Models +@using Angor.Shared.Services @inject ILogger Logger; - +@inject INetworkService networkService;
@@ -112,6 +113,7 @@ { Logger.LogError(failure, failure.Message); ShowErrorMessage(failure.Message); + networkService.HandleException(failure); } else { diff --git a/src/Angor/Shared/Services/INetworkService.cs b/src/Angor/Shared/Services/INetworkService.cs index 47de17f2..8420cca5 100644 --- a/src/Angor/Shared/Services/INetworkService.cs +++ b/src/Angor/Shared/Services/INetworkService.cs @@ -11,4 +11,6 @@ public interface INetworkService List GetRelays(); void CheckAndHandleError(HttpResponseMessage httpResponseMessage); + + void HandleException(Exception exception); } \ No newline at end of file diff --git a/src/Angor/Client/Services/NetworkService.cs b/src/Angor/Shared/Services/NetworkService.cs similarity index 86% rename from src/Angor/Client/Services/NetworkService.cs rename to src/Angor/Shared/Services/NetworkService.cs index 5d177472..e7089ea0 100644 --- a/src/Angor/Client/Services/NetworkService.cs +++ b/src/Angor/Shared/Services/NetworkService.cs @@ -1,12 +1,8 @@ using System.Net; -using Angor.Shared; -using Microsoft.JSInterop; -using System.Net.WebSockets; using Angor.Shared.Models; -using Angor.Shared.Services; -using Angor.Client.Storage; +using Microsoft.Extensions.Logging; -namespace Angor.Client.Services +namespace Angor.Shared.Services { public class NetworkService : INetworkService { @@ -126,5 +122,20 @@ public void CheckAndHandleError(HttpResponseMessage httpResponseMessage) } } } + + public void HandleException(Exception exception) + { + if (exception is HttpRequestException httpRequestException) + { + // dont block the caller + Task.Run(async () => + { + // this code will run outside of the UI thread, however wasm is single threaded so it will block the UI, + // we should consider using a cancellation token in this case that will not cancel after a shorter time to block the UI for too long + + await CheckServices(true); + }); + } + } } }