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

IExceptionHandlerFeature is not available in CustomizeProblemDetails when using UseDeveloperExceptionPage #47060

Closed
1 task done
bjornen77 opened this issue Mar 6, 2023 · 4 comments
Assignees
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware help wanted Up for grabs. We would accept a PR to help resolve this issue

Comments

@bjornen77
Copy link
Contributor

bjornen77 commented Mar 6, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The IExceptionHandlerFeature is not available in CustomizeProblemDetails when using "UseDeveloperExceptionPage". It is, however, available when using "UseExceptionHandler()". Is there a reason for this? It would be good to be able to customize the problem details also when using the DeveloperExceptionPage.

Expected Behavior

The IExceptionHandlerFeature is available when using DeveloperExceptionPage

Steps To Reproduce

Try the "/" endpoint from swagger.

using Microsoft.AspNetCore.Diagnostics;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddProblemDetails(configure =>
    configure.CustomizeProblemDetails = (context) =>
    {
        if (context.HttpContext.Features.Get<IExceptionHandlerFeature>() is IExceptionHandlerFeature exceptionHandlerFeature)
        {
            Console.WriteLine("This will not print when using UseDeveloperExceptionPage()");
        }
    }
);

var app = builder.Build();

app.UseDeveloperExceptionPage();

app.UseSwagger();
app.UseSwaggerUI();

app.MapGet("/", () =>
{
    throw new Exception();
});

app.Run();

Exceptions (if any)

No response

.NET Version

7.0.201

Anything else?

No response

@captainsafia
Copy link
Member

The IExceptionHandlerFeature is not available in CustomizeProblemDetails when using "UseDeveloperExceptionPage". It is, however, available when using "UseExceptionHandler()". Is there a reason for this?

The exception handling middleware has the potential to re-execute the middleware pipeline so the IExceptionHandlerFeature is used to store details that need to be persisted across re-executions. We could definitely set it in the developer-exception page but there isn't a strong technically requirement to.

Question: Is there a particular reason you are wanting to use the IExceptionHandlerFeature?

The only info that you can get in the IExceptionHandlerFeature that isn't already in the ProblemDetailsContext is the original Exception that you access via IExceptionHandlerFeature.Error.

@captainsafia captainsafia added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Mar 20, 2023
@ghost
Copy link

ghost commented Mar 20, 2023

Hi @bjornen77. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@bjornen77
Copy link
Contributor Author

bjornen77 commented Mar 20, 2023

I want to be able to customize the problem details extensions property with information from the original exception. The reason is that a receiving application always should be able read the customized information in the problem details response.

I know that i can create a custom exception handler also for development purposes, but it would be nice to use the built-in one. Also, it would also be more consistent(when "CustomizeProblemDetails" is called in both cases)

I think that it might be easy to set the IExceptionHandlerFeature in the developer-exception-page. I could try to make a PR for it if wanted.

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Mar 20, 2023
@captainsafia
Copy link
Member

@bjornen77 Thanks! A PR is definitely welcome. It helps to reuse the existing feature here.

@captainsafia captainsafia added help wanted Up for grabs. We would accept a PR to help resolve this issue and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Mar 20, 2023
bjornen77 added a commit to bjornen77/aspnetcore that referenced this issue Mar 31, 2023
When using the developer exception page, the exception handler
feature is now set before invoking the problem details service.
This makes it possible to get the original exception when using
the problem details service if wanted.

Fixes dotnet#47060
@ghost ghost locked as resolved and limited conversation to collaborators May 11, 2023
@amcasey amcasey added area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware and removed area-runtime labels Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlesware help wanted Up for grabs. We would accept a PR to help resolve this issue
Projects
None yet
Development

No branches or pull requests

5 participants
@captainsafia @javiercn @amcasey @bjornen77 and others