GithubHelp home page GithubHelp logo

Comments (5)

sliekens avatar sliekens commented on May 19, 2024 1

Here's how I use it:

options.MapStatusCode = statusCode => new StatusCodeProblemDetails(statusCode)
{
    Type = "about:blank"
};

or

options.MapStatusCode = statusCode => new StatusCodeProblemDetails(statusCode)
{
    Type = null // not recommended if JsonSerializerSettings.NullValueHandling is "Include"
};

from middleware.

khellang avatar khellang commented on May 19, 2024

So my recommendation is to just leave off the problem type and let it default to about:blank for generic errors. At least make it configurable using ProblemDetailsOptions.

I just included the httpstatuses.com links because I saw that https://github.com/zendframework/zend-problem-details uses them. The upcoming MVC feature uses RFC7231 links as the type. I'm not sure if there's any right or wrong answer here. IMO it's better to go for something that you can dereference to find out more, rather than just about:blank, but we could definitely make this configurable.

from middleware.

sliekens avatar sliekens commented on May 19, 2024

Okay then I think it's fine to leave httpstatuses.com as the default problem type, but make it configurable so I can override it.

Here's what I imagine that would look like:

app.UseProblemDetails(x =>
{
    // This is the default behavior; only include exception details in a development environment.
    x.IncludeExceptionDetails = ctx => Environment.IsDevelopment();

    // This will map NotImplementedException to the 501 Not Implemented status code.
    x.Map<NotImplementedException>(ex => new ExceptionProblemDetails(ex, StatusCodes.Status501NotImplemented));

    // NEW: This will map status code results to problem details
    // **unless** the status code is the product of Map<T, ExceptionProblemDetails>
    x.MapStatusCode(statusCode => new ProblemDetails
    {
        Type = "about:blank",
        Status = statusCode,
        Title = ReasonPhrases.GetReasonPhrase(statusCode)
    });
});

from middleware.

khellang avatar khellang commented on May 19, 2024

Alright, I pushed v1.2.0 which adds a new property MapStatusCode to ProblemDetailsOptions. It's basically just a Func<int, ProblemDetails> that allows you to map a status code to a problem response.

from middleware.

sliekens avatar sliekens commented on May 19, 2024

Perfect, thanks.

from middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.