Skip to content

Set IExceptionHandlerFeature on DeveloperExceptionPageMiddleware #52688

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ public async Task Invoke(HttpContext context)
context.Response.StatusCode = 500;
}

await _exceptionHandler(new ErrorContext(context, ex));
var errorContext = new ErrorContext(context, ex);

SetExceptionHandlerFeatures(errorContext);
await _exceptionHandler(errorContext);

const string eventName = "Microsoft.AspNetCore.Diagnostics.UnhandledException";
if (_diagnosticSource.IsEnabled(eventName))
Expand Down Expand Up @@ -200,17 +203,12 @@ private async Task DisplayExceptionContent(ErrorContext errorContext)
{
var httpContext = errorContext.HttpContext;

if (_problemDetailsService is not null)
{
SetExceptionHandlerFeatures(errorContext);
}

if (_problemDetailsService == null || !await _problemDetailsService.TryWriteAsync(new()
{
HttpContext = httpContext,
ProblemDetails = CreateProblemDetails(errorContext, httpContext),
Exception = errorContext.Exception
}))
{
HttpContext = httpContext,
ProblemDetails = CreateProblemDetails(errorContext, httpContext),
Exception = errorContext.Exception
}))
{
httpContext.Response.ContentType = "text/plain; charset=utf-8";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,47 @@ public async Task Metrics_ExceptionThrown_Unhandled_Reported()
m => AssertRequestException(m, "System.InvalidOperationException", "unhandled"));
}

[Fact]
public async Task ExceptionFeatureSetOnDeveloperExceptionPage()
{
// Arrange
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseTestServer()
.Configure(app =>
{
app.Use(async (context, next) =>
{
await next();

var exceptionHandlerFeature = context.Features.GetRequiredFeature<IExceptionHandlerPathFeature>();

Assert.NotNull(exceptionHandlerFeature);
Assert.Equal("Test exception", exceptionHandlerFeature.Error.Message);
Assert.Equal(context.Request.Path, exceptionHandlerFeature.Path);
});
app.UseExceptionHandler(exceptionApp =>
{
exceptionApp.Run(context => Task.CompletedTask);
});
app.Run(context =>
{
throw new Exception("Test exception");
});

});
}).Build();

await host.StartAsync();

var server = host.GetTestServer();
var request = new HttpRequestMessage(HttpMethod.Get, "/path");

var response = await server.CreateClient().SendAsync(request);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't forcing the asserts to run. Move asserts into the main path of the test:

Suggested change
// Arrange
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseTestServer()
.Configure(app =>
{
app.Use(async (context, next) =>
{
await next();
var exceptionHandlerFeature = context.Features.GetRequiredFeature<IExceptionHandlerPathFeature>();
Assert.NotNull(exceptionHandlerFeature);
Assert.Equal("Test exception", exceptionHandlerFeature.Error.Message);
Assert.Equal(context.Request.Path, exceptionHandlerFeature.Path);
});
app.UseExceptionHandler(exceptionApp =>
{
exceptionApp.Run(context => Task.CompletedTask);
});
app.Run(context =>
{
throw new Exception("Test exception");
});
});
}).Build();
await host.StartAsync();
var server = host.GetTestServer();
var request = new HttpRequestMessage(HttpMethod.Get, "/path");
var response = await server.CreateClient().SendAsync(request);
// Arrange
var tcs = new TaskCompletionSource<IExceptionHandlerPathFeature>(TaskCreationOptions.RunContinuationsAsynchronously);
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseTestServer()
.Configure(app =>
{
app.Use(async (context, next) =>
{
await next();
var exceptionHandlerFeature = context.Features.GetRequiredFeature<IExceptionHandlerPathFeature>();
tcs.SetResult(exceptionHandlerFeature);
});
app.UseExceptionHandler(exceptionApp =>
{
exceptionApp.Run(context => Task.CompletedTask);
});
app.Run(context =>
{
throw new Exception("Test exception");
});
});
}).Build();
await host.StartAsync();
var server = host.GetTestServer();
var request = new HttpRequestMessage(HttpMethod.Get, "/path");
var response = await server.CreateClient().SendAsync(request);
var feature = await tcs.Task;
Assert.NotNull(feature);
Assert.Equal("Test exception", feature.Error.Message);
Assert.Equal(context.Request.Path, feature.Path);

(note: changed in a text field, not might not compile, and/or have bugs)

}

private static void AssertRequestException(CollectedMeasurement<long> measurement, string exceptionName, string result, string handler = null)
{
Assert.Equal(1, measurement.Value);
Expand Down