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

Make it easier to return Problem Details responses for server errors #8537

Closed
DamianEdwards opened this issue Mar 14, 2019 · 4 comments
Closed
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Done This issue has been fixed enhancement This issue represents an ask for new feature or an enhancement to an existing one

Comments

@DamianEdwards
Copy link
Member

Add a set of new Problem helper methods on ControllerBase, that populates a ProblemDetails type with relevant values for simple returning of errors through the MVC response pipeline, e.g.:

private ObjectResult Problem()
{
    return Problem(detail: null);
}
 
private ObjectResult Problem(string detail)
{
    var problemDetails = new ProblemDetails
    {
        Title = "An error occurred while processing your request.",
        Detail = detail,
        Status = 500
    };
 
    problemDetails.Extensions.Add("traceId", Activity.Current?.Id ?? HttpContext.TraceIdentifier);
    return Problem(problemDetails);
}
 
private ObjectResult Problem(ProblemDetails problemDetails)
{
    return new ObjectResult(problemDetails) { StatusCode = problemDetails.Status };
}

@rynowak

@DamianEdwards DamianEdwards added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 14, 2019
@mkArtakMSFT mkArtakMSFT added this to the 3.0.0-preview6 milestone Mar 20, 2019
@mkArtakMSFT mkArtakMSFT added 1 - Ready enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Mar 20, 2019
@thomaslevesque
Copy link
Member

I was looking for something like this today.

Currently, there's no convenient way to return a problem details response with the ability to specify the title/detail/instance/etc. Methods like NotFound(), BadRequest(), etc produce a ProblemDetails with a default title and no detail.

A Problem() method would indeed be very useful. I think an overload that accepts the status code would be useful too, since you might want to return problem details for errors other than 500.


Suggestion: also create a ProblemDetailsResult class, which could handle things like:

  • adding a traceId
  • setting appropriate content types ("application/problem+json" and "application/problem+xml")
  • setting title and type to default values if they're not set

All these things are currently done in the default InvalidModelStateResponseFactory and in ProblemDetailsClientErrorFactory


@thomaslevesque
Copy link
Member

BTW, I'm interested in implementing this, if there isn't someone already working on it.

@khellang
Copy link
Member

  • setting appropriate content types ("application/problem+json" and "application/problem+xml")

This is already done for you, if you use an ObjectResult:

https://github.com/aspnet/AspNetCore/blob/5ca92305c2e6452e2add9a2eb115963896c6fe85/src/Mvc/Mvc.Core/src/Infrastructure/ObjectResultExecutor.cs#L135-L139

@thomaslevesque
Copy link
Member

Oh, nice! Thanks @khellang

pranavkm added a commit that referenced this issue Jul 18, 2019
* Introduce ControllerBase.Problem and ValidationProblem overload
  that accepts optional parameters
* Consistently use ClientErrorData when generating ProblemDetails
* Clean-up InvalidModelStateResponseFactory initialization.

Fixes #8537
pranavkm added a commit that referenced this issue Jul 19, 2019
* Introduce ControllerBase.Problem and ValidationProblem overload
  that accepts optional parameters
* Consistently use ClientErrorData when generating ProblemDetails
* Clean-up InvalidModelStateResponseFactory initialization.

Fixes #8537
pranavkm added a commit that referenced this issue Jul 23, 2019
* Introduce ControllerBase.Problem and ValidationProblem overload
  that accepts optional parameters
* Consistently use ClientErrorData when generating ProblemDetails
* Clean-up InvalidModelStateResponseFactory initialization.

Fixes #8537
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.0.0-preview9, 3.1.0 Jul 31, 2019
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.1.0, 3.0.0-preview9 Jul 31, 2019
pranavkm added a commit that referenced this issue Aug 5, 2019
* Add helper methods on ControllerBase to return ProblemDetails

* Introduce ControllerBase.Problem and ValidationProblem overload
  that accepts optional parameters
* Consistently use ClientErrorData when generating ProblemDetails
* Clean-up InvalidModelStateResponseFactory initialization.

Fixes #8537
@pranavkm pranavkm added Done This issue has been fixed and removed Working labels Aug 5, 2019
@pranavkm pranavkm closed this as completed Aug 6, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Done This issue has been fixed enhancement This issue represents an ask for new feature or an enhancement to an existing one
Projects
None yet
Development

No branches or pull requests

5 participants