Skip to content

Commit

Permalink
🐛 (Button): exception thrown in click event wasn't being caught by th…
Browse files Browse the repository at this point in the history
…e error handler (#2139)
  • Loading branch information
capdiem authored Sep 9, 2024
1 parent 3397e41 commit e97a417
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
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);
}
}
}

0 comments on commit e97a417

Please sign in to comment.