Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

.NET and Blazor 8 - handling exceptions without ErrorBoundary on Interactive Server pages?

$
0
0

I have a Blazor & .NET 8 Web App which uses interactive server mode to render its pages.

Is it possible to add a global handler which will catch unhandled exceptions before the default yellow "An error has occurred. reload" error message is displayed?

I do believe I can achieve this with ErrorBoundary but have been asked to investigate and consider other options as well, for flexibility's sake.

I looked into middleware, tried the code below (taken from UseExceptionHandler Blazor server-side not working) and it doesn't work. I expect this is for the same reason as the most upvoted answer in the linked question.

If the ErrorBoundary (or deriving from ErrorBoundary) is the only way to catch all unhandled exceptions thrown as a result of user action in ISR, please state that categorically.

Here is my middleware handler.

public class ExceptionHandlingMiddleware{    private readonly RequestDelegate _next;    private string _path;    private readonly ILogger<ExceptionHandlingMiddleware> _logger;    public ExceptionHandlingMiddleware(ILogger<ExceptionHandlingMiddleware> logger, RequestDelegate next, string path)    {        _next = next;        _path = path;        _logger = logger;    }    public async Task InvokeAsync(HttpContext context)    {        try        {            await _next(context);        }        catch (System.Exception ex)        {            _logger.LogError(ex, "Failed to invoke {Message}", ex.Message);            context.Response.Redirect(_path);        }    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>