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 all 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,51 @@ public async Task Metrics_ExceptionThrown_Unhandled_Reported()
m => AssertRequestException(m, "System.InvalidOperationException", "unhandled"));
}

[Fact]
public async Task ExceptionFeatureSetOnDeveloperExceptionPage()
{
// 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("/path", feature.Path);
}

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