Skip to content
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

Set exception handler feature in developer exception page #47554

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -88,6 +88,22 @@ private static ExtensionsExceptionJsonContext CreateSerializationContext(JsonOpt
return new ExtensionsExceptionJsonContext(new JsonSerializerOptions(jsonOptions.SerializerOptions));
}

private static void SetExceptionHandlerFeatures(ErrorContext errorContext)
{
var httpContext = errorContext.HttpContext;

ExceptionHandlerFeature exceptionHandlerFeature = new()
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
Error = errorContext.Exception,
Path = httpContext.Request.Path.ToString(),
Endpoint = httpContext.GetEndpoint(),
RouteValues = httpContext.Features.Get<IRouteValuesFeature>()?.RouteValues
};

httpContext.Features.Set<IExceptionHandlerFeature>(exceptionHandlerFeature);
httpContext.Features.Set<IExceptionHandlerPathFeature>(exceptionHandlerFeature);
}

/// <summary>
/// Process an individual request.
/// </summary>
Expand Down Expand Up @@ -184,6 +200,11 @@ 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) }))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -14,6 +18,124 @@ namespace Microsoft.AspNetCore.Diagnostics;

public class DeveloperExceptionPageMiddlewareTest
{
[Fact]
public async Task ExceptionHandlerFeatureIsAvailableInCustomizeProblemDetailsWhenUsingExceptionPage()
{
// Arrange
using var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddRouting();
services.AddProblemDetails(configure =>
{
configure.CustomizeProblemDetails = (context) =>
{
var feature = context.HttpContext.Features.Get<IExceptionHandlerFeature>();
context.ProblemDetails.Extensions.Add("OriginalExceptionMessage", feature?.Error.Message);
context.ProblemDetails.Extensions.Add("EndpointDisplayName", feature?.Endpoint?.DisplayName);
context.ProblemDetails.Extensions.Add("RouteValue", feature?.RouteValues?["id"]);
context.ProblemDetails.Extensions.Add("Path", feature?.Path);
};
});
})
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseTestServer()
.Configure(app =>
{
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseEndpoints(endpoint =>
{
endpoint.MapGet("/test/{id}", (int id) =>
{
throw new Exception("Test exception");
});
});
});
}).Build();

await host.StartAsync();

var server = host.GetTestServer();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/test/1");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

// Act
var response = await server.CreateClient().SendAsync(request);

// Assert
var body = await response.Content.ReadFromJsonAsync<ProblemDetails>();
var originalExceptionMessage = ((JsonElement)body.Extensions["OriginalExceptionMessage"]).GetString();
var endpointDisplayName = ((JsonElement)body.Extensions["EndpointDisplayName"]).GetString();
var routeValue = ((JsonElement)body.Extensions["RouteValue"]).GetString();
var path = ((JsonElement)body.Extensions["Path"]).GetString();
Assert.Equal("Test exception", originalExceptionMessage);
Assert.Contains("/test/{id}", endpointDisplayName);
Assert.Equal("1", routeValue);
Assert.Equal("/test/1", path);
}

[Fact]
public async Task ExceptionHandlerPathFeatureIsAvailableInCustomizeProblemDetailsWhenUsingExceptionPage()
{
// Arrange
using var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddRouting();
services.AddProblemDetails(configure =>
{
configure.CustomizeProblemDetails = (context) =>
{
var feature = context.HttpContext.Features.Get<IExceptionHandlerPathFeature>();
context.ProblemDetails.Extensions.Add("OriginalExceptionMessage", feature?.Error.Message);
context.ProblemDetails.Extensions.Add("EndpointDisplayName", feature?.Endpoint?.DisplayName);
context.ProblemDetails.Extensions.Add("RouteValue", feature?.RouteValues?["id"]);
context.ProblemDetails.Extensions.Add("Path", feature?.Path);
};
});
})
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseTestServer()
.Configure(app =>
{
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseEndpoints(endpoint =>
{
endpoint.MapGet("/test/{id}", (int id) =>
{
throw new Exception("Test exception");
});
});
});
}).Build();

await host.StartAsync();

var server = host.GetTestServer();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/test/1");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

// Act
var response = await server.CreateClient().SendAsync(request);

// Assert
var body = await response.Content.ReadFromJsonAsync<ProblemDetails>();
var originalExceptionMessage = ((JsonElement)body.Extensions["OriginalExceptionMessage"]).GetString();
var endpointDisplayName = ((JsonElement)body.Extensions["EndpointDisplayName"]).GetString();
var routeValue = ((JsonElement)body.Extensions["RouteValue"]).GetString();
var path = ((JsonElement)body.Extensions["Path"]).GetString();
Assert.Equal("Test exception", originalExceptionMessage);
Assert.Contains("/test/{id}", endpointDisplayName);
Assert.Equal("1", routeValue);
Assert.Equal("/test/1", path);
}

[Fact]
public async Task UnhandledErrorsWriteToDiagnosticWhenUsingExceptionPage()
{
Expand Down