Fluxor
Fluxor copied to clipboard
StoreInitializer.UnhandledException for action subscribers?
Currently we have StoreInitializer.UnhandledException
which is called for exceptions occurring in effects since they are async.
Is there any reason why the same is not used for action subscribers?
The reason I ask is because there are number of cases where an action subscriber is used to start async operations. And since events are synchronous it leads to missing unobserved exceptions. Moving these to effects sometimes is inconvenient and complicates things.
Using AppDomain.CurrentDomain.UnhandledException
won't do much since exceptions can't be ignored in this event and still get passed to internal exception handler (resulting in a yellow bar at the bottom).
Your action subscriber code should be synchronous.
If you do any asynchronous work here then it's up to you to deal with problems :)
My advice would be to InvokeAsync your code. That way it'll be inside the Blazor UI context and should show exceptions.
Unfortunately InvokeAsync
won't help here since in the end it is also not awaited hence not observed.
I guess I'll move it all to effects then.
Thanks.
You don't have to await it. You just using InvokeAsync to ensure it is thread-safe
_ = InvokeAsync(async() => { await DoWhatever(); StateHasChanged(); });
Thanks, but it doesn't make any difference regarding exception handling. If it's not awaited in the end then exceptions are not handled.
For example:
private void Test()
{
_ = InvokeAsync(async () =>
{
await Task.Yield();
throw new NotImplementedException();
});
}
When this code is used inside any simple button click event the exception is not raised anywhere. However, when awaited it is logged in a console. Not handled, but at least processed and shown.
This is a fault with Blazor. I registered it, but a bot closed it https://github.com/dotnet/aspnetcore/issues/19271
Looks like the desired Blazor behaviour.
I'd welcome a PR if you'd like to submit one ;)
I was going to )) Just need to find some time to make it right.