Skip to content

DefaultApiProblemDetailsWriter.WriteAsync loses original context.ProblemDetails.Exception value #63753

@wasker

Description

@wasker

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm trying to add raw exception into problem response in CustomizeProblemDetails in dev environment, however, context.Exception is always null.

Expected Behavior

Exception should not be null.

Steps To Reproduce

builder.Services
    .AddProblemDetails()
    .AddExceptionHandler(options =>
    {
        options.StatusCodeSelector = exception => exception switch
        {
            FailedToAuthenticateTokenException _ => StatusCodes.Status401Unauthorized,
            _ => (int) HttpStatusCode.InternalServerError
        };
    });

builder.Services.Configure<ProblemDetailsOptions>(options =>
{
    var dumpException = builder.Environment.IsDevelopment();

    options.CustomizeProblemDetails = context =>
    {
        var exception = context.Exception;
        if (exception is not null)
        {
            if (dumpException)
            {
                context.ProblemDetails.Extensions["exception"] = exception.ToString();
            }
        }
    };
});

var app = builder.Build();

app.UseExceptionHandler();

Exceptions (if any)

No response

.NET Version

9.0.305

Anything else?

Exception handler actually adds exception to problem details. However, DefaultApiProblemDetailsWriter then re-creates problem details and ignores exception which was in original data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-problem-details

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions