liveBook · Manning

Problem Details

As explained in Building Web APIs with ASP.NET Core

ProblemDetails is an object model for representing machine-readable details about an HTTP API error response.

ProblemDetails represents machine-readable details for an HTTP API error. It produces an RFC 7807-compliant JSON response so clients receive a consistent status, title, type, and optional diagnostic information. section 6.2.4

An exception endpoint can return Results.Problem() directly, or construct a ProblemDetails instance to add the exception message and a trace identifier before returning the response. section 6.2.4

A customized error response can:

  • Set the HTTP status explicitly, such as 500 for an unhandled exception. section 6.2.4
  • Copy the exception message into detail for a more informative client response. section 6.2.4
  • Add extensions such as traceId to connect a client error with server diagnostics. section 6.2.4
  • Provide a place for logging, notifications, and exception-specific handling. section 6.2.4

Customized exception response

A request to /error/test can produce a JSON body containing type, title, status 500, detail "test", and a traceId. This replaces a minimal generic error with information useful for client handling and investigation. section 6.2.4

app.MapGet("/error",
    [EnableCors("AnyOrigin")]
    [ResponseCache(NoStore = true)] (HttpContext context) =>           #1
    {
        var exceptionHandler =
context.Features.Get<IExceptionHandlerPathFeature>();      #2
        
// TODO: logging, sending notifications, and more              #3
        
        var details = new ProblemDetails();
        details.Detail = exceptionHandler?.Error.Message;              #4
        details.Extensions["traceId"] =
            System.Diagnostics.Activity.Current?.Id 
              ?? context.TraceIdentifier;
        details.Type =
            "https://tools.ietf.org/html/rfc7231#section-6.6.1";
        details.Status = StatusCodes.Status500InternalServerError;
        return Results.Problem(details);
    });

Inspect the complete Minimal API handler that retrieves the exception, populates ProblemDetails, adds traceId, and returns Results.Problem(details). section 6.2.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Demonstrates default and customized responses, but stops short of implementing exception-specific status mappings.
Examples
Uses an /error endpoint, an /error/test exception, and JSON containing status, detail, type, title, and traceId.
What it assumes
Assumes familiarity with HTTP status codes, exceptions, Minimal API endpoints, and JSON response handling.
What you can do after
Operate/configure ASP.NET Core exception handling to return standardized, traceable JSON error responses.
How it connects
Connects HTTP API errors with RFC 7807, exception handling middleware, Minimal APIs, and ModelState validation.

Also explained in

Compare how 2 books explain Problem Details →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.