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

fix(Button): exception thrown in click event wasn't being caught by the error handler #2139

Merged
merged 1 commit into from
Sep 9, 2024
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
5 changes: 1 addition & 4 deletions src/Masa.Blazor/Components/Button/MButton.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ protected async Task HandleOnClick(MouseEventArgs args)
await Js.InvokeVoidAsync(JsInteropConstants.Blur, Ref);
}

if (OnClick.HasDelegate)
{
await OnClick.InvokeAsync(args);
}
await InvokeCallbackAsync(OnClick, args);

await ToggleAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Masa.Blazor/Components/Card/MCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected override void OnParametersSet()
(Tag, Attributes) = _router.GenerateRouteLink();

Attributes["ripple"] = Ripple && IsClickable;
Attributes["onclick"] = OnClick;
Attributes["onclick"] = EventCallback.Factory.Create<MouseEventArgs>(this, args => InvokeCallbackAsync(OnClick, args));
}

private static Block _block = new("m-card");
Expand Down
27 changes: 27 additions & 0 deletions src/Masa.Blazor/Core/MasaComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,31 @@ private async Task CallStateHasChangedOnAsyncCompletion(Task task)
}

#endregion

/// <summary>
/// Invoke a callback event and use the error handler to catch exceptions
/// when an error occurs.
/// </summary>
/// <param name="callback"></param>
/// <param name="args"></param>
/// <typeparam name="TEventArgs"></typeparam>
protected async Task InvokeCallbackAsync<TEventArgs>(EventCallback<TEventArgs> callback, TEventArgs args)
{
try
{
if (callback.HasDelegate)
{
await callback.InvokeAsync(args);
}
}
catch (Exception e)
{
if (ErrorHandler is null)
{
throw;
}

await ErrorHandler.HandleExceptionAsync(e);
}
}
}
Loading