Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/FloppyShelf/Problemize/llms.txt

Use this file to discover all available pages before exploring further.

Exception handler that converts exceptions into standardized problem details responses following RFC 9457.

Namespace

FloppyShelf.Problemize.Services

Class declaration

public sealed class ExceptionHandler : IExceptionHandler

Constructor

Creates a new instance of the ExceptionHandler class.
public ExceptionHandler(
    IProblemDetailsService problemDetailsService,
    IStatusCodeMapper statusCodeMapper)

Parameters

problemDetailService
IProblemDetailsService
required
The service used to write problem details responses.
statusCodeMapper
IStatusCodeMapper
required
The component responsible for mapping exceptions to HTTP status codes.

Methods

TryHandleAsync

Attempts to handle the exception and generate a problem details response.
public async ValueTask<bool> TryHandleAsync(
    HttpContext httpContext,
    Exception exception,
    CancellationToken cancellationToken)

Parameters

httpContext
HttpContext
required
The current HTTP context.
exception
Exception
required
The exception to handle.
cancellationToken
CancellationToken
required
Cancellation token for async operations.

Returns

result
ValueTask<bool>
Returns true if the exception was handled and a response was written; otherwise, false.

Behavior

This method:
  1. Sets the HTTP response status code based on the exception using the status code mapper
  2. Creates a ValidationProblemDetails for ValidationException instances
  3. Creates a standard ProblemDetails for all other exceptions
  4. Writes the problem details response using the problem details service

Problem details structure

For validation exceptions:
  • Title: “An error occured while validating your request”
  • Detail: The exception message
  • Type: The exception type name
For all other exceptions:
  • Title: “An error occured while processing your request”
  • Detail: The exception message
  • Type: The exception type name

Example

// This class is registered automatically by UseExceptionHandling()
// and handles exceptions globally in your application.

// When an exception occurs:
throw new ValidationException("Email is required");

// The response will be:
// Status: 400 Bad Request
// {
//   "type": "ValidationException",
//   "title": "An error occured while validating your request",
//   "detail": "Email is required",
//   "status": 400,
//   "instance": "POST /api/users",
//   "requestId": "0HN7...",
//   "activityId": "00-..."
// }

Build docs developers (and LLMs) love