-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Support Razor Component endpoints for UseExceptionHandler #50287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@SteveSandersonMS we can put some metadata in the HttpContext to skip initializing the services a second time. Dump all root components and "start fresh" to render the error page in the existing context. sequenceDiagram
participant Browser
participant Renderer
participant ErrorHandler
Browser->>Renderer: Render Page
Renderer->>Renderer: Initialize services
Browser->>Renderer: Render page
Renderer->>Renderer: Error
Renderer->>ErrorHandler: Catch error
ErrorHandler->>Renderer: Render error page
alt HandlingError
Renderer->>Renderer: Avoid reinitializing services
end
Renderer->>Browser: HTML Error
This does not have to be specific to the error handler, we can put a marker on the renderer and have a way to "reset it" (dispose all root components and start over) |
That's how I started implementing a solution yesterday before realising the broader problems listed above. |
I'm able to catch common errors like 500 (Internal Server Error) and 402 (Not found) in SSR and redirect to an error page using this: app.UseStatusCodePagesWithRedirects("/error/{0}"); But somehow it doesn't work for 401 (Unauthorized). It is not caught by the handler. |
@SteveSandersonMS I did this? Reopen if this is tracking something else. |
Currently it doesn't work to do this:
... where
/Error
corresponds to a Razor Component like:In practice what happens is that:
ExceptionHandlerMiddlewareImpl
catches the original unhandled exception, changes the endpoint to/Error
(a Razor Component endpoint), and re-runs the pipelineEndpointHtmlRenderer.InitializeStandardComponentServicesAsync
which tries to initialize the same scoped services for a second time.NavigationManager.Initialize
(as reported at HttpNavigationManager already initialized on Blazor SSR. #49456)Difficulties
This isn't straightforward to resolve for two main reasons:
RazorComponentEndpointInvoker
to guarantee we create a new DI scope for the error endpoint. This would be inconsistent with MVC etc but at least safer. And would solve the double-initialization exceptions./MyComponentThatThrows
throws an unhandled exceptionError.razor
, which uses the sameLayout.razor
, which contains a@rendermode=Server
router and<script src="blazor.web.js"></script>
/MyComponentThatThrows
and hence now re-runs the same component that throws a second timeThe text was updated successfully, but these errors were encountered: