refactored ErrorBoundary implementation to support logging

This commit is contained in:
Shaun Walker 2021-11-22 16:11:44 -05:00
parent 59850f4869
commit 19be77ed49
6 changed files with 161 additions and 143 deletions

View File

@ -65,8 +65,8 @@
}
catch (Exception ex)
{
await logger.LogError(ex, "An Error Occurred Loading Html/Text Content. " + ex.Message);
AddModuleMessage(ex.Message, MessageType.Error);
await logger.LogError(ex, "Error Loading Content {Error}", ex.Message);
AddModuleMessage(Localizer["Error.Content.Load"], MessageType.Error);
}
}
@ -91,7 +91,7 @@
await HtmlTextService.AddHtmlTextAsync(htmltext);
}
await logger.LogInformation("Html/Text Content Saved {HtmlText}", htmltext);
await logger.LogInformation("Content Saved {HtmlText}", htmltext);
NavigationManager.NavigateTo(NavigateUrl());
}
catch (Exception ex)

View File

@ -35,8 +35,8 @@
}
catch (Exception ex)
{
await logger.LogError(ex, "An Error Occurred Loading Html/Text Content. " + ex.Message);
AddModuleMessage(ex.Message, MessageType.Error);
await logger.LogError(ex, "Error Loading Content {Error}", ex.Message);
AddModuleMessage(Localizer["Error.Content.Load"], MessageType.Error);
}
}
}

View File

@ -117,7 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Error.Content.Load" xml:space="preserve">
<value>An Error Occurred Loading Content</value>
</data>
<data name="Error.Content.Save" xml:space="preserve">
<value>Error Saving Content</value>
<value>An Error Occurred Saving Content</value>
</data>
</root>

View File

@ -59,7 +59,7 @@
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="root">
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
@ -120,4 +120,7 @@
<data name="Edit.Action" xml:space="preserve">
<value>Edit</value>
</data>
<data name="Error.Content.Load" xml:space="preserve">
<value>An Error Occurred Loading Content</value>
</data>
</root>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
@ -124,6 +124,6 @@
<value>Module Type Is Invalid For {0}</value>
</data>
<data name="Error.Module.Exception" xml:space="preserve">
<value>Program error in module {0}</value>
<value>An Unexpected Error Has Occurred</value>
</data>
</root>

View File

@ -1,11 +1,10 @@
@namespace Oqtane.UI
@inject IStringLocalizer<ModuleInstance> Localizer
@inject ILogService LoggingService
@inherits ErrorBoundary
<ErrorBoundary>
<ErrorContent>
<ModuleMessage Message="@ErrorMessage(context)" Type="@MessageType.Error"/>
</ErrorContent>
<ChildContent>
@if (CurrentException is null)
{
<ModuleMessage Message="@_message" Type="@_messageType"/>
@if (ModuleType != null)
{
@ -15,11 +14,18 @@
<div class="app-progress-indicator"></div>
}
}
</ChildContent>
</ErrorBoundary>
}
else
{
@if (!string.IsNullOrEmpty(_error))
{
<ModuleMessage Message="@_error" Type="@MessageType.Error"/>
}
}
@code {
private string _message;
private string _error;
private MessageType _messageType;
private bool _progressIndicator = false;
@ -34,8 +40,6 @@
private ModuleMessage ModuleMessage { get; set; }
private RenderFragment DynamicComponent { get; set; }
protected override void OnParametersSet()
{
_message = "";
@ -78,15 +82,23 @@
StateHasChanged();
}
private string ErrorMessage(Exception context)
protected override async Task OnErrorAsync(Exception exception)
{
var message = string.Format(Localizer["Error.Module.Exception"], ModuleState.ModuleDefinitionName);
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
{
return $"{message}<br />{context}";
// retrieve friendly localized error
_error = Localizer["Error.Module.Exception"];
// log error
int? userId = (PageState.User != null) ? PageState.User.UserId : null;
string category = GetType().AssemblyQualifiedName;
string feature = Utilities.GetTypeNameLastSegment(category, 1);
await LoggingService.Log(null, ModuleState.PageId, ModuleState.ModuleId, userId, category, feature, LogFunction.Other, LogLevel.Error, exception, "An Unexpected Error Has Occurred In {ModuleDefinitionName}: {Error}", ModuleState.ModuleDefinitionName, exception.Message);
await base.OnErrorAsync(exception);
return;
}
return message;
public new void Recover()
{
_error = "";
base.Recover();
}
}