Git Product home page Git Product logo

ProblemDetails

Build Quality Gate Status Coverage NuGet

Problem

By default, the ASP.NET MVC Core framework doesn't return consistent error responses across different statuses. For example, the framework will return a status 500 with an empty body when encountering an internal server error. Model validation errors will send back a JSON body, but the field names don't follow the casing rules. This package is meant to unify the responses.

Solution

All response codes above 400 will have a nice JSON response, for example:

Status 404 Not Found

{
  "type": "https://httpstatuses.com/404",
  "title": "Error: Not Found",
  "status": 404
}

Status 400 Bad Request (note that the field names are camel-cased)

{
  "errors": {
    "requiredField": ["The requiredField field is required."]
  },
  "type": "https://httpstatuses.com/400",
  "title": "Error: Bad Request",
  "status": 400
}

Status 500 Internal Server Error

{
  "type": "https://httpstatuses.com/500",
  "title": "Error: Internal Server Error",
  "status": 500,
  "exception": "System.Exception: Testing 500\n   at Sample.WebApi.Controllers.WeatherForecastController.Get(Int32 id) in /Users/hanneskarask/dev/Problem.Details/samples/Sample.WebApi/Controllers/WeatherForecastController.cs:line 42\n   at lambda_method3(Closure , Object , Object[] )..."
}

(exception is only visible when explicitly turned on, i.e. in dev environments)

You can also override the title values and map custom exceptions.

Getting started

  1. Install the package
Install-Package Problem.Details

Or via the .NET Core command line interface:

dotnet add package Problem.Details
  1. Call services.AddProblemDetails() in services configure method:
public void ConfigureServices(IServiceCollection services)
{
    services.AddProblemDetails();
  1. Call app.UseProblemDetails() in app Configure method:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseProblemDetails(configure => configure
        // Optional: Override title for Bad Request
        .MapStatusToTitle(HttpStatusCode.BadRequest, "One or more validation errors occurred")
        // Optional: Map your custom exception
        .MapException<NotFoundException>(HttpStatusCode.NotFound)
        // Optional: Show detailed error
        .ShowErrorDetails(env.IsDevelopment())
    );

Check the sample project or browse source

problemdetails's Projects

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.