Skip to content

Add custom exception-to-status-code mapping to GlobalErrorHandlingMiddleware - #23

Merged
perkops merged 5 commits into
mainfrom
feature/custom-exception-type-mapping
Jan 6, 2026
Merged

perkops merged 5 commits into
mainfrom
feature/custom-exception-type-mapping

Conversation

@perkops

@perkops perkops commented Jan 6, 2026

Copy link
Copy Markdown
Member

Summary

Add the ability to map custom exception types to specific HTTP status codes in GlobalErrorHandlingMiddleware. This enables domain-specific exceptions to be automatically translated to appropriate HTTP responses.

Changes

New Feature: Custom Exception Mapping

  • Add MapException<T>(HttpStatusCode) fluent API to GlobalErrorHandlingOptions
  • Add ExceptionMappingResolver for immutable, thread-safe status code resolution
  • Support inheritance: derived exception types inherit base type mappings
  • Custom mappings take precedence over built-in defaults
  • Type-safe API using HttpStatusCode enum

Usage Example

app.UseGlobalErrorHandler(options =>
{
    options.MapException<OrderNotFoundException>(HttpStatusCode.NotFound);
    options.MapException<DuplicateResourceException>(HttpStatusCode.Conflict);

    // For exotic status codes not in HttpStatusCode enum
    options.MapException<TeapotException>((HttpStatusCode)StatusCodes.Status418ImATeapot);
});

Sample & Documentation

- Add GET /api/test/teapot endpoint demonstrating HTTP 418 response
- Add TeapotException as example custom exception
- Update README with custom exception mapping documentation

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a custom exception-to-status-code mapping feature to GlobalErrorHandlingMiddleware, enabling domain-specific exceptions to be automatically translated to appropriate HTTP responses. The implementation includes a fluent API, thread-safe resolver, and inheritance support.

  • Introduces MapException<T> fluent API for custom exception mappings
  • Implements ExceptionMappingResolver for immutable, thread-safe status code resolution
  • Adds comprehensive test coverage and sample/documentation demonstrating HTTP 418 "I'm a teapot" response

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/Directory.Build.props Updated test dependency package versions
test/Atc.Rest.MinimalApi.Tests/Middleware/GlobalErrorHandlingMiddlewareTests.cs Added test cases for custom exception mapping, inheritance, precedence, and method chaining
test/Atc.Rest.MinimalApi.Tests/GlobalUsings.cs Added System.Net and test exceptions namespace imports
test/Atc.Rest.MinimalApi.Tests/Exceptions/TeapotTestException.cs Created test exception for demonstrating custom mapping
test/.editorconfig Disabled CA1515 analyzer rule
src/Atc.Rest.MinimalApi/Options/GlobalErrorHandlingOptions.cs Added MapException methods and BuildResolver for custom exception mappings
src/Atc.Rest.MinimalApi/Options/ExceptionMappingResolver.cs Implemented immutable resolver with inheritance support for exception-to-status-code mapping
src/Atc.Rest.MinimalApi/Middleware/GlobalErrorHandlingMiddleware.cs Refactored to use ExceptionMappingResolver instead of switch-based mapping
src/Atc.Rest.MinimalApi/GlobalUsings.cs Added System.Collections.Frozen namespace
src/Atc.Rest.MinimalApi/Atc.Rest.MinimalApi.csproj Updated package dependency versions
sample/test/Directory.Build.props Updated sample test dependency package versions
sample/src/Demo.Web/Demo.Web.csproj Updated sample web dependency package versions
sample/src/Demo.Domain/Demo.Domain.csproj Updated sample domain dependency package versions
sample/src/Demo.AppHost/Demo.AppHost.csproj Updated Aspire SDK version
sample/src/Demo.Api/GlobalUsings.cs Added System.Net and exceptions namespace imports
sample/src/Demo.Api/Extensions/WebApplicationExtensions.cs Demonstrated TeapotException custom mapping to HTTP 418
sample/src/Demo.Api/Demo.Api.csproj Updated sample API dependency package versions
sample/src/Demo.Api.Contracts/GlobalUsings.cs Added exceptions namespace import
sample/src/Demo.Api.Contracts/Exceptions/TeapotException.cs Created sample exception for HTTP 418 demonstration
sample/src/Demo.Api.Contracts/EndpointDefinitions/TestEndpointDefinition.cs Added /api/test/teapot endpoint demonstrating custom exception mapping
sample/src/Demo.Api.Contracts/EndpointDefinitions/SwaggerGroupNames.cs Added Test group constant for Swagger organization
sample/src/Demo.Api.Contracts/Demo.Api.Contracts.csproj Updated sample contracts dependency package versions
sample/Directory.Build.props Updated sample code analyzer package versions
README.md Added custom exception mapping documentation with usage examples and default mappings table
Directory.Build.props Updated code analyzer package versions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Atc.Rest.MinimalApi/Options/GlobalErrorHandlingOptions.cs
Comment thread src/Atc.Rest.MinimalApi/Options/ExceptionMappingResolver.cs
Comment thread sample/src/Demo.Api/GlobalUsings.cs
@perkops
perkops merged commit 4a243e0 into main Jan 6, 2026
4 checks passed
@perkops
perkops deleted the feature/custom-exception-type-mapping branch January 6, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants