Repository navigation
Add custom exception-to-status-code mapping to GlobalErrorHandlingMiddleware - #23
Merged
Merged
Conversation
added 5 commits
January 6, 2026 14:04
There was a problem hiding this comment.
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
ExceptionMappingResolverfor 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.
davidkallesen
approved these changes
Jan 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
MapException<T>(HttpStatusCode)fluent API toGlobalErrorHandlingOptionsExceptionMappingResolverfor immutable, thread-safe status code resolutionHttpStatusCodeenumUsage Example