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.

Interface to map exceptions to HTTP status codes. Implement this interface to create custom exception mapping logic.

Namespace

FloppyShelf.Problemize.Interfaces

Interface declaration

public interface IStatusCodeMapper

Methods

GetStatusCode

Maps an exception to an appropriate HTTP status code.
int GetStatusCode(Exception exception)

Parameters

exception
Exception
required
The exception to evaluate.

Returns

statusCode
int
The corresponding HTTP status code.

Implementations

Problemize provides a default implementation:
  • StatusCodeMapper - Maps common .NET exceptions to appropriate HTTP status codes

Example implementation

using FloppyShelf.Problemize.Interfaces;

public class CustomStatusCodeMapper : IStatusCodeMapper
{
    public int GetStatusCode(Exception exception)
    {
        return exception switch
        {
            // Custom exceptions
            ProductNotFoundException => StatusCodes.Status404NotFound,
            InsufficientStockException => StatusCodes.Status409Conflict,
            PaymentFailedException => StatusCodes.Status402PaymentRequired,
            
            // Business validation errors
            BusinessRuleException => StatusCodes.Status422UnprocessableEntity,
            
            // Authentication/Authorization
            InvalidTokenException => StatusCodes.Status401Unauthorized,
            InsufficientPermissionsException => StatusCodes.Status403Forbidden,
            
            // Fallback to default
            _ => StatusCodes.Status500InternalServerError
        };
    }
}

Usage

Register your custom mapper when configuring Problemize:
var builder = WebApplication.CreateBuilder(args);

// Register with custom mapper
builder.Services.UseExceptionHandling(new CustomStatusCodeMapper());

var app = builder.Build();
app.UseExceptionHandling();

Design considerations

  • Return status codes from the Microsoft.AspNetCore.Http.StatusCodes class for consistency
  • Consider using specific 4xx codes for client errors and 5xx codes for server errors
  • Provide a sensible default case (typically 500 Internal Server Error)
  • Order exception types from most specific to least specific in switch expressions
  • Document your custom mappings for API consumers