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.

Default implementation that maps known exceptions to appropriate HTTP status codes.

Namespace

FloppyShelf.Problemize.Services

Class declaration

public sealed class StatusCodeMapper : IStatusCodeMapper

Methods

GetStatusCode

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

Parameters

exception
Exception
required
The exception to evaluate.

Returns

statusCode
int
The corresponding HTTP status code.

Exception mappings

The default mapper uses the following exception-to-status-code mappings:
Exception TypeHTTP Status CodeCode
ArgumentNullException400 Bad RequestStatusCodes.Status400BadRequest
ArgumentOutOfRangeException400 Bad RequestStatusCodes.Status400BadRequest
InvalidOperationException400 Bad RequestStatusCodes.Status400BadRequest
ValidationException400 Bad RequestStatusCodes.Status400BadRequest
FormatException400 Bad RequestStatusCodes.Status400BadRequest
OverflowException400 Bad RequestStatusCodes.Status400BadRequest
NullReferenceException400 Bad RequestStatusCodes.Status400BadRequest
UnauthorizedAccessException401 UnauthorizedStatusCodes.Status401Unauthorized
KeyNotFoundException404 Not FoundStatusCodes.Status404NotFound
FileNotFoundException404 Not FoundStatusCodes.Status404NotFound
DirectoryNotFoundException404 Not FoundStatusCodes.Status404NotFound
NotSupportedException405 Method Not AllowedStatusCodes.Status405MethodNotAllowed
TimeoutException408 Request TimeoutStatusCodes.Status408RequestTimeout
OutOfMemoryException500 Internal Server ErrorStatusCodes.Status500InternalServerError
StackOverflowException500 Internal Server ErrorStatusCodes.Status500InternalServerError
NotImplementedException501 Not ImplementedStatusCodes.Status501NotImplemented
All other exceptions500 Internal Server ErrorStatusCodes.Status500InternalServerError

Example

var mapper = new StatusCodeMapper();

// Returns 404
var code = mapper.GetStatusCode(new KeyNotFoundException("User not found"));

// Returns 400
var code = mapper.GetStatusCode(new ValidationException("Invalid email"));

// Returns 500
var code = mapper.GetStatusCode(new Exception("Unexpected error"));

Custom mapper

To override the default mappings, implement the IStatusCodeMapper interface:
public class CustomStatusCodeMapper : IStatusCodeMapper
{
    public int GetStatusCode(Exception exception)
    {
        return exception switch
        {
            MyCustomException => StatusCodes.Status418ImATeapot,
            ArgumentException => StatusCodes.Status422UnprocessableEntity,
            _ => StatusCodes.Status500InternalServerError
        };
    }
}

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

Build docs developers (and LLMs) love