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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,11 +1,10 @@
@namespace Oqtane.UI @namespace Oqtane.UI
@inject IStringLocalizer<ModuleInstance> Localizer @inject IStringLocalizer<ModuleInstance> Localizer
@inject ILogService LoggingService
@inherits ErrorBoundary
<ErrorBoundary> @if (CurrentException is null)
<ErrorContent> {
<ModuleMessage Message="@ErrorMessage(context)" Type="@MessageType.Error"/>
</ErrorContent>
<ChildContent>
<ModuleMessage Message="@_message" Type="@_messageType"/> <ModuleMessage Message="@_message" Type="@_messageType"/>
@if (ModuleType != null) @if (ModuleType != null)
{ {
@ -15,11 +14,18 @@
<div class="app-progress-indicator"></div> <div class="app-progress-indicator"></div>
} }
} }
</ChildContent> }
</ErrorBoundary> else
{
@if (!string.IsNullOrEmpty(_error))
{
<ModuleMessage Message="@_error" Type="@MessageType.Error"/>
}
}
@code { @code {
private string _message; private string _message;
private string _error;
private MessageType _messageType; private MessageType _messageType;
private bool _progressIndicator = false; private bool _progressIndicator = false;
@ -34,8 +40,6 @@
private ModuleMessage ModuleMessage { get; set; } private ModuleMessage ModuleMessage { get; set; }
private RenderFragment DynamicComponent { get; set; }
protected override void OnParametersSet() protected override void OnParametersSet()
{ {
_message = ""; _message = "";
@ -78,15 +82,23 @@
StateHasChanged(); StateHasChanged();
} }
protected override async Task OnErrorAsync(Exception exception)
private string ErrorMessage(Exception context)
{ {
var message = string.Format(Localizer["Error.Module.Exception"], ModuleState.ModuleDefinitionName); // retrieve friendly localized error
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) _error = Localizer["Error.Module.Exception"];
{ // log error
return $"{message}<br />{context}"; 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();
} }
} }