Skip to content
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 @@ -4,6 +4,7 @@
#nullable enable

using System.Diagnostics;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -108,6 +109,7 @@ private void ApplyProblemDetailsDefaults(HttpContext httpContext, ProblemDetails
problemDetails.Extensions["traceId"] = traceId;
}

_configure?.Invoke(new() { HttpContext = httpContext!, ProblemDetails = problemDetails });
var exception = httpContext?.Features.Get<IExceptionHandlerFeature>()?.Error;
_configure?.Invoke(new() { HttpContext = httpContext!, ProblemDetails = problemDetails, Exception = exception });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Microsoft.AspNetCore.Mvc.RouteAttribute</Description>
<Reference Include="Microsoft.AspNetCore.Authentication.Core" />
<Reference Include="Microsoft.AspNetCore.Authorization.Policy" />
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Http" />
<Reference Include="Microsoft.AspNetCore.Http.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -169,6 +172,65 @@ public void CreateValidationProblemDetails_WithTitleAndInstance()
});
}

[Fact]
public void CreateProblemDetails_IncludesExceptionFromExceptionHandlerFeature()
{
// Regression test for https://github.com/dotnet/aspnetcore/issues/65697
// CustomizeProblemDetails should receive the exception when one is available.
Exception? receivedException = null;
var expectedException = new InvalidOperationException("BOOM!");

var options = new ApiBehaviorOptions();
new ApiBehaviorOptionsSetup().Configure(options);

var problemDetailsOptions = new ProblemDetailsOptions
{
CustomizeProblemDetails = context =>
{
receivedException = context.Exception;
}
};

var factory = new DefaultProblemDetailsFactory(
Options.Create(options),
Options.Create(problemDetailsOptions));

var httpContext = GetHttpContext();
httpContext.Features.Set<IExceptionHandlerFeature>(
new ExceptionHandlerFeature { Error = expectedException });

factory.CreateProblemDetails(httpContext, statusCode: 500);

Assert.Same(expectedException, receivedException);
}

[Fact]
public void CreateProblemDetails_ExceptionIsNullWhenNoExceptionHandlerFeature()
{
Exception? receivedException = new Exception("should be overwritten");

var options = new ApiBehaviorOptions();
new ApiBehaviorOptionsSetup().Configure(options);

var problemDetailsOptions = new ProblemDetailsOptions
{
CustomizeProblemDetails = context =>
{
receivedException = context.Exception;
}
};

var factory = new DefaultProblemDetailsFactory(
Options.Create(options),
Options.Create(problemDetailsOptions));

var httpContext = GetHttpContext();

factory.CreateProblemDetails(httpContext, statusCode: 400);

Assert.Null(receivedException);
}

private static DefaultHttpContext GetHttpContext()
{
return new DefaultHttpContext
Expand All @@ -183,4 +245,9 @@ private static ProblemDetailsFactory GetProblemDetails()
new ApiBehaviorOptionsSetup().Configure(options);
return new DefaultProblemDetailsFactory(Options.Create(options));
}

private sealed class ExceptionHandlerFeature : IExceptionHandlerFeature
{
public Exception Error { get; init; } = default!;
}
}
Loading