-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Blazor] Error page support for Server Side Rendered Razor Component Applications #50550
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9857eef
Add the ability to create a separate DI scope
javiercn b6f5296
Supress interactive rendering on error scenarios
javiercn f1357aa
Revert sample changes
javiercn a632314
Feedback
javiercn a7d5c30
Add error page
javiercn 2634e67
Add E2E test
javiercn 50cc0e0
Fix api baseline
javiercn e42d93d
Turn scope by default for testing purposes
javiercn 3b58a65
Revert "Turn scope by default for testing purposes"
javiercn 4faecfc
Prevent form handling logic to run during exception handling
javiercn 4468cd5
Update template baselines
javiercn 571c1cf
Fixes
javiercn dd2d0e2
Add missing baseline files
javiercn 4a6c3be
Cleanups and feedback
javiercn 8ae14cb
Update E2E test
javiercn 94427b7
Fix markers
javiercn b8cab2e
Introduce a delay on the test
javiercn f7c08bf
Feedback
javiercn 1708135
Add BOM
javiercn ca2f604
Update src/Components/test/testassets/Components.TestServer/RazorComp…
javiercn 2c28c95
Update src/Components/test/testassets/Components.TestServer/RazorComp…
javiercn a431b64
Update src/Components/test/testassets/Components.TestServer/RazorComp…
javiercn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@page "/Error" | ||
javiercn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@using System.Diagnostics | ||
|
||
<PageTitle>Error</PageTitle> | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> | ||
|
||
@code{ | ||
[CascadingParameter] public HttpContext? HttpContext { get; set; } | ||
|
||
public string? RequestId { get; set; } | ||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
||
protected override void OnInitialized() => | ||
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Components/test/E2ETest/ServerRenderingTests/ErrorHandlingTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; | ||
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; | ||
using TestServer; | ||
using Components.TestServer.RazorComponents; | ||
using Microsoft.AspNetCore.E2ETesting; | ||
using Xunit.Abstractions; | ||
using OpenQA.Selenium; | ||
|
||
namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests; | ||
|
||
public class ErrorHandlingTest(BrowserFixture browserFixture, BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>> serverFixture, ITestOutputHelper output) | ||
: ServerTestBase<BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>>>(browserFixture, serverFixture, output) | ||
{ | ||
|
||
[Fact] | ||
public async Task RendersExceptionFromComponent() | ||
{ | ||
GoTo("Throws?suppress-autostart=true"); | ||
|
||
Browser.Equal("Error", () => Browser.Title); | ||
|
||
Assert.Collection( | ||
Browser.FindElements(By.CssSelector(".text-danger")), | ||
item => Assert.Equal("Error.", item.Text), | ||
item => Assert.Equal("An error occurred while processing your request.", item.Text)); | ||
javiercn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Browser.Equal("False", () => Browser.FindElement(By.Id("is-interactive-server")).Text); | ||
javiercn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Browser.Click(By.Id("call-blazor-start")); | ||
await Task.Delay(3000); | ||
Browser.Exists(By.Id("blazor-started")); | ||
Browser.Equal("False", () => Browser.FindElement(By.Id("is-interactive-server")).Text); | ||
} | ||
|
||
private void GoTo(string relativePath) | ||
{ | ||
Navigate($"{ServerPathBase}/{relativePath}"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Error.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
@page "/Error" | ||
@layout ErrorLayout | ||
@using System.Diagnostics | ||
|
||
<PageTitle>Error</PageTitle> | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> | ||
|
||
@code { | ||
[CascadingParameter] | ||
public HttpContext? HttpContext { get; set; } | ||
|
||
private string? RequestId { get; set; } | ||
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
||
protected override void OnInitialized() => | ||
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/ErrorLayout.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@inherits Microsoft.AspNetCore.Components.LayoutComponentBase | ||
|
||
<ServerInteractiveCounter /> | ||
|
||
<div> | ||
@Body | ||
</div> |
11 changes: 11 additions & 0 deletions
11
...mponents/test/testassets/Components.TestServer/RazorComponents/Pages/PageThatThrows.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@page "/Throws" | ||
|
||
<PageTitle>Page that throws</PageTitle> | ||
|
||
<p>This page throws during OnInitialize to showcase error handling via UseExceptionHandler.</p> | ||
|
||
@code | ||
{ | ||
protected override void OnInitialized() => | ||
throw new InvalidOperationException("This page throws on purpose."); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.