Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing ErrorBoundary in ModuleInstance component #1815

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Oqtane.Client/Resources/UI/ModuleInstance.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,7 @@
<data name="Error.Module.InvalidType" xml:space="preserve">
<value>Module Type Is Invalid For {0}</value>
</data>
<data name="Error.Module.Exception" xml:space="preserve">
<value>Program error in module {0}</value>
</data>
</root>
88 changes: 51 additions & 37 deletions Oqtane.Client/UI/ModuleInstance.razor
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
@namespace Oqtane.UI
@inject IStringLocalizer<ModuleInstance> Localizer

<ModuleMessage Message="@_message" Type="@_messagetype" />
@DynamicComponent
@if (_progressindicator)
{
<div class="app-progress-indicator"></div>
}
<ErrorBoundary>
<ErrorContent>
<ModuleMessage Message="@ErrorMessage(context)" Type="@MessageType.Error"/>
</ErrorContent>
<ChildContent>
<ModuleMessage Message="@_message" Type="@_messageType"/>
@if (ModuleType != null)
{
<DynamicComponent Type="@ModuleType" Parameters="@ModuleParameters"></DynamicComponent>
@if (_progressIndicator)
{
<div class="app-progress-indicator"></div>
}
}
</ChildContent>
</ErrorBoundary>

@code {
private string _message;
private MessageType _messagetype;
private bool _progressindicator = false;
private MessageType _messageType;
private bool _progressIndicator = false;

private Type ModuleType { get; set; }
private IDictionary<string, object> ModuleParameters { get; set; }

[CascadingParameter]
protected PageState PageState { get; set; }
Expand All @@ -26,53 +39,54 @@
protected override void OnParametersSet()
{
_message = "";

DynamicComponent = builder =>
if (!string.IsNullOrEmpty(ModuleState.ModuleType))
{
Type moduleType = null;
if (!string.IsNullOrEmpty(ModuleState.ModuleType))
{
moduleType = Type.GetType(ModuleState.ModuleType);

if (moduleType != null)
{
builder.OpenComponent(0, moduleType);
builder.AddAttribute(1, "ModuleInstance", this);
builder.CloseComponent();
}
else
{
// module does not exist with typename specified
_message = string.Format(Localizer["Error.Module.InvalidName"], Utilities.GetTypeNameLastSegment(ModuleState.ModuleType, 0));
_messagetype = MessageType.Error;
}
}
else
ModuleType = Type.GetType(ModuleState.ModuleType);
if (ModuleType != null)
{
_message = string.Format(Localizer["Error.Module.InvalidType"], ModuleState.ModuleDefinitionName);
_messagetype = MessageType.Error;
ModuleParameters = new Dictionary<string, object> { { "ModuleInstance", this } };
return;
}

};
// module does not exist with typename specified
_message = string.Format(Localizer["Error.Module.InvalidName"], Utilities.GetTypeNameLastSegment(ModuleState.ModuleType, 0));
_messageType = MessageType.Error;
}
else
{
_message = string.Format(Localizer["Error.Module.InvalidType"], ModuleState.ModuleDefinitionName);
_messageType = MessageType.Error;
}
}

public void AddModuleMessage(string message, MessageType type)
{
_message = message;
_messagetype = type;
_progressindicator = false;
_messageType = type;
_progressIndicator = false;
StateHasChanged();
}

public void ShowProgressIndicator()
{
_progressindicator = true;
_progressIndicator = true;
StateHasChanged();
}

public void HideProgressIndicator()
{
_progressindicator = false;
_progressIndicator = false;
StateHasChanged();
}


private string ErrorMessage(Exception context)
{
var message = string.Format(Localizer["Error.Module.Exception"], ModuleState.ModuleDefinitionName);
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
{
return $"{message}<br />{context}";
}
return message;
}

}