Skip to content

Commit

Permalink
handle network errors in the backgorund
Browse files Browse the repository at this point in the history
  • Loading branch information
dangershony committed Nov 10, 2023
1 parent 45f9e9f commit 38d7fe2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Angor/Client/Shared/NotificationComponent.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@using Angor.Shared.Models
@using Angor.Shared.Services

@inject ILogger<NotificationComponent> Logger;

@inject INetworkService networkService;

<div class="row mt-4">
<div class="col">
Expand Down Expand Up @@ -112,6 +113,7 @@
{
Logger.LogError(failure, failure.Message);
ShowErrorMessage(failure.Message);
networkService.HandleException(failure);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/Angor/Shared/Services/INetworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface INetworkService
List<SettingsUrl> GetRelays();

void CheckAndHandleError(HttpResponseMessage httpResponseMessage);

void HandleException(Exception exception);
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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);
});
}
}
}
}

0 comments on commit 38d7fe2

Please sign in to comment.