liveBook · Manning

Error response

As explained in ASP.NET Core in Action, 3e

An error status code response is an HTTP response whose status code indicates that the requested operation failed or could not be completed.

An error response tells an API consumer that a requested operation failed or could not be completed. In a minimal API application, it can result from an endpoint returning an error status code, missing route handling, authentication middleware, or an exception. Consistent APIs represent these failures with Problem Details JSON. section 5.3.3

Handle the two main error classes differently:

  • Exceptions are caught by ExceptionHandlerMiddleware or DeveloperExceptionPageMiddleware and can be converted automatically to Problem Details after registering IProblemDetailsService with AddProblemDetails(). section 5.3.3
  • Error status code responses without a body can be completed by StatusCodePagesMiddleware, which adds a Problem Details response body. section 5.3.3

Returning a Problem Details body for a 404

Register Problem Details, add StatusCodePagesMiddleware, and return Results.NotFound() from an endpoint. The middleware supplies the response body automatically, giving consumers a consistent JSON error shape. section 5.3.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains exceptions, status codes, and middleware configuration, stopping short of detailing the Problem Details payload fields.
Examples
Demonstrates a thrown exception and Results.NotFound() with AddProblemDetails, ExceptionHandlerMiddleware, and StatusCodePagesMiddleware.
What it assumes
Assumes familiarity with minimal API endpoints and middleware while distinguishing exceptions from ordinary error status responses.
What you can do after
Configure minimal APIs to return Problem Details for exceptions and bodyless error status codes.
How it connects
Connects error responses with Problem Details, exceptions, middleware, authentication, and HTTP status codes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.