Improved soft error boundary

This commit is contained in:
Marcel Baumgartner
2023-08-14 18:22:20 +02:00
parent a0c2b45a61
commit 4c7ffe6714
2 changed files with 46 additions and 38 deletions

View File

@@ -1 +0,0 @@
Moonlight

View File

@@ -12,8 +12,9 @@
@inject AlertService AlertService @inject AlertService AlertService
@inject ConfigService ConfigService @inject ConfigService ConfigService
@inject SmartTranslateService SmartTranslateService @inject SmartTranslateService SmartTranslateService
@inject NavigationManager NavigationManager
@if (Crashed) @if (HardCrashed)
{ {
<div class="card card-flush h-md-100"> <div class="card card-flush h-md-100">
<div class="card-body d-flex flex-column justify-content-between mt-9 bgi-no-repeat bgi-size-cover bgi-position-x-center pb-0"> <div class="card-body d-flex flex-column justify-content-between mt-9 bgi-no-repeat bgi-size-cover bgi-position-x-center pb-0">
@@ -30,6 +31,16 @@
</div> </div>
</div> </div>
} }
else if (SoftCrashed)
{
<div class="card card-body bg-danger mb-5">
<span class="text-center">
@(ErrorMessage)
</span>
</div>
@ChildContent
}
else else
{ {
@ChildContent @ChildContent
@@ -37,8 +48,23 @@ else
@code @code
{ {
private bool Crashed = false; private bool HardCrashed = false;
private bool SoftCrashed = false;
private string ErrorMessage = "";
protected override void OnInitialized()
{
NavigationManager.LocationChanged += OnPathChanged;
}
private void OnPathChanged(object? sender, LocationChangedEventArgs e)
{
if (SoftCrashed)
SoftCrashed = false;
StateHasChanged();
}
protected override async Task OnErrorAsync(Exception exception) protected override async Task OnErrorAsync(Exception exception)
{ {
if (ConfigService.DebugMode) if (ConfigService.DebugMode)
@@ -50,74 +76,57 @@ else
{ {
if (displayException.DoNotTranslate) if (displayException.DoNotTranslate)
{ {
await AlertService.Error( await SoftCrash(displayException.Message);
displayException.Message
);
} }
else else
{ {
await AlertService.Error( await SoftCrash(SmartTranslateService.Translate(displayException.Message));
SmartTranslateService.Translate(displayException.Message)
);
} }
} }
else if (exception is CloudflareException cloudflareException) else if (exception is CloudflareException cloudflareException)
{ {
await AlertService.Error( await SoftCrash(SmartTranslateService.Translate("Error from cloudflare: ") + cloudflareException.Message);
SmartTranslateService.Translate("Error from cloudflare api"),
cloudflareException.Message
);
} }
else if (exception is WingsException wingsException) else if (exception is WingsException wingsException)
{ {
await AlertService.Error( await SoftCrash(SmartTranslateService.Translate("Error from wings: ") + wingsException.Message);
SmartTranslateService.Translate("Error from wings"),
wingsException.Message
);
//TODO: Error log service
Logger.Warn($"Wings exception status code: {wingsException.StatusCode}"); Logger.Warn($"Wings exception status code: {wingsException.StatusCode}");
} }
else if (exception is DaemonException daemonException) else if (exception is DaemonException daemonException)
{ {
await AlertService.Error( await SoftCrash(SmartTranslateService.Translate("Error from daemon: ") + daemonException.Message);
SmartTranslateService.Translate("Error from daemon"),
daemonException.Message
);
Logger.Warn($"Wings exception status code: {daemonException.StatusCode}"); Logger.Warn($"Wings exception status code: {daemonException.StatusCode}");
} }
else if (exception is ModrinthException modrinthException) else if (exception is ModrinthException modrinthException)
{ {
await AlertService.Error( await SoftCrash(SmartTranslateService.Translate("Error from modrinth: ") + modrinthException.Message);
SmartTranslateService.Translate("Error from modrinth"),
modrinthException.Message
);
} }
else if (exception is CloudPanelException cloudPanelException) else if (exception is CloudPanelException cloudPanelException)
{ {
await AlertService.Error( await SoftCrash(SmartTranslateService.Translate("Error from cloudpanel: ") + cloudPanelException.Message);
SmartTranslateService.Translate("Error from cloud panel"),
cloudPanelException.Message
);
} }
else if (exception is NotImplementedException) else if (exception is NotImplementedException)
{ {
await AlertService.Error(SmartTranslateService.Translate("This function is not implemented")); await SoftCrash(SmartTranslateService.Translate("This function is not implemented"));
} }
else if (exception is StripeException stripeException) else if (exception is StripeException stripeException)
{ {
await AlertService.Error( await SoftCrash(SmartTranslateService.Translate("Error from stripe: ") + stripeException.Message);
SmartTranslateService.Translate("Unknown error from stripe"),
stripeException.Message
);
} }
else else
{ {
Logger.Warn(exception); Logger.Warn(exception);
Crashed = true; HardCrashed = true;
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }
} }
private Task SoftCrash(string message)
{
SoftCrashed = true;
ErrorMessage = message;
return Task.CompletedTask;
}
} }