Asim Shahzad
Asim Shahzad

Reputation: 71

Whole blazor web app stop working when exception occured

Please advice me any suitable solution for the following issue,

when the blazor application throws any exception, the whole application goes down and no link is working, until I can run the application through studio again.

what to do with this issue?

thanks & best regards

Edited

(In order to provide requested info)

Steps to reproduce:

  1. Create a blazorserverside app:

  2. Modify IncrementCount

At Counter.razor:

void IncrementCount()
{
    currentCount += 1;
    _ = 0 / (5-currentCount);  // <-- force error when currentCount is 5.
}
  1. Push Click Me button for 5 times to raise error.

  2. Try to navigate to other app pages (Home, Fetch Data) nothing happens because it fails silently on client.

Additional info

On Startup.cs errors are configured:

app.UseExceptionHandler("/errors");

The stack trace errors:

Unhandled exception rendering component: Attempted to divide by zero.
System.DivideByZeroException: Attempted to divide by zero.
   at blaex.Pages.Counter.IncrementCount() in /home/dani/tmp/blaex/Pages/Counter.razor:line 27
   at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T](MulticastDelegate delegate, T arg)
   at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg)
   at Microsoft.AspNetCore.Components.Rendering.Renderer.DispatchEventAsync(Int32 eventHandlerId, UIEventArgs eventArgs)

Upvotes: 3

Views: 6699

Answers (1)

dani herrera
dani herrera

Reputation: 51685

Edited Jul'2021

Fortunately, we now have official literature in this regard: Handle errors in ASP.NET Core Blazor apps :

When a Blazor app isn't functioning properly during development, receiving detailed error information from the app assists in troubleshooting and fixing the issue. When an error occurs, Blazor apps display a light yellow bar at the bottom of the screen:

  • During development, the bar directs you to the browser console, where you can see the exception.
  • In production, the bar notifies the user that an error has occurred and recommends refreshing the browser.

The UI for this error handling experience is part of the Blazor project templates.

On docs they are Blazor Server detailed circuit errors and, Global exception handling, Log errors with a persistent provider, Places where errors may occur, Advanced scenarios and Additional resources.

Also, I write here some samples about how to catch exceptions:

Sample 1:

For your code:

void IncrementCount()
{
    currentCount += 1;
    _ = 0 / (5-currentCount);  // <-- force error when currentCount is 5.
}

Solution is:

void IncrementCount()
{
    currentCount += 1;
    try
    {
        _ = 0 / (5-currentCount);
    }
    catch (DivideByZeroException e)
    {
        // handling exception
    }
}

Sample 2:

For DivideByZeroException on .razor page:

<h1> @( (0 / (5-currentCount) ).ToString()  ) </h1>

They are not solution at this time.

Edited work around by Mister Magoo: There is a solution for Sample 2: try..catch - but it's not very practical to do that to all your markup

<h1>
    @try
    {
        @:@((0 / (5 - currentCount)).ToString())
    }
    catch (Exception ex)
    {
        @:@ex.Message;
    }
</h1>

Upvotes: 2

Related Questions