Include exception in ProblemDetailsContext for controller error responses#65817
Open
BloodShop wants to merge 1 commit intodotnet:mainfrom
Open
Include exception in ProblemDetailsContext for controller error responses#65817BloodShop wants to merge 1 commit intodotnet:mainfrom
BloodShop wants to merge 1 commit intodotnet:mainfrom
Conversation
…nses When using ExceptionHandlerMiddleware with controllers, the CustomizeProblemDetails callback receives a ProblemDetailsContext with a null Exception. This happens because DefaultProblemDetailsFactory creates a new context internally without pulling the exception from the HttpContext features. The Minimal API path (DefaultProblemDetailsWriter) works correctly because it passes the original context — which already has the exception set by the middleware — straight through to the callback. The fix: in DefaultProblemDetailsFactory.ApplyProblemDetailsDefaults, resolve the exception from IExceptionHandlerFeature on the HttpContext before invoking the customize callback. When no exception handler feature is present (e.g., validation errors, manual status codes), the exception remains null as before. Changes: - DefaultProblemDetailsFactory: read exception from IExceptionHandlerFeature and set it on the ProblemDetailsContext - Add Diagnostics.Abstractions reference to Mvc.Core - Add tests verifying exception propagation in both scenarios Fixes dotnet#65697
Contributor
|
Thanks for your PR, @@BloodShop. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
3 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When using
ExceptionHandlerMiddlewarewith controllers andCustomizeProblemDetails, theProblemDetailsContext.Exceptionis alwaysnull. The same setup with Minimal APIs correctly populates the exception.Reproduction
context.Exceptionis alwaysnulleven though the exception is available.Root Cause
Two different code paths handle problem details writing:
Minimal APIs (
DefaultProblemDetailsWriter): CallsCustomizeProblemDetailsdirectly with the originalProblemDetailsContextfrom the middleware — which already has theExceptionset. Works correctly.Controllers (
DefaultApiProblemDetailsWriter→DefaultProblemDetailsFactory): The factory'sApplyProblemDetailsDefaultscreates a newProblemDetailsContextfor the callback, but never sets theExceptionproperty:The Fix
In
DefaultProblemDetailsFactory.ApplyProblemDetailsDefaults, resolve the exception fromIExceptionHandlerFeature(which theExceptionHandlerMiddlewaresets on theHttpContext) and include it in the context:When no
IExceptionHandlerFeatureis present (validation errors, manual status codes, etc.),exceptionisnull— same behavior as before.Changes
DefaultProblemDetailsFactory.csIExceptionHandlerFeatureonHttpContext.FeaturesProblemDetailsContextin the customize callbackMicrosoft.AspNetCore.Mvc.Core.csprojMicrosoft.AspNetCore.Diagnostics.Abstractions(forIExceptionHandlerFeature)ProblemDetailsFactoryTest.cs(tests)IExceptionHandlerFeatureis presentnullwhen no feature is presentBehavior After Fix
context.Exception == null❌context.Exception == <thrown>✅context.Exception == <thrown>✅context.Exception == nullcontext.Exception == nullBreaking Changes
None. The only change is that
ProblemDetailsContext.Exceptionis now populated where it was previouslynull. Code that checked forcontext.Exception is not nullwill now correctly enter that branch.Fixes #65697