Fluxor icon indicating copy to clipboard operation
Fluxor copied to clipboard

StoreInitializer.UnhandledException for action subscribers?

Open uhfath opened this issue 2 years ago • 7 comments

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).

uhfath avatar Jul 20 '22 16:07 uhfath

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.

mrpmorris avatar Jul 20 '22 18:07 mrpmorris

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.

uhfath avatar Jul 21 '22 05:07 uhfath

You don't have to await it. You just using InvokeAsync to ensure it is thread-safe

_ = InvokeAsync(async() => { await DoWhatever(); StateHasChanged(); });

mrpmorris avatar Jul 21 '22 11:07 mrpmorris

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.

uhfath avatar Jul 21 '22 13:07 uhfath

This is a fault with Blazor. I registered it, but a bot closed it https://github.com/dotnet/aspnetcore/issues/19271

mrpmorris avatar Jul 21 '22 14:07 mrpmorris

Looks like the desired Blazor behaviour.

I'd welcome a PR if you'd like to submit one ;)

mrpmorris avatar Jul 27 '22 09:07 mrpmorris

I was going to )) Just need to find some time to make it right.

uhfath avatar Jul 27 '22 09:07 uhfath