Skip to content

feat(mediator): add ExceptionHandlingBehavior pipeline#38

Merged
senrecep merged 1 commit into
mainfrom
feat/mediator-exception-handling-behavior
May 31, 2026
Merged

feat(mediator): add ExceptionHandlingBehavior pipeline#38
senrecep merged 1 commit into
mainfrom
feat/mediator-exception-handling-behavior

Conversation

@senrecep
Copy link
Copy Markdown
Owner

Summary

  • Adds ExceptionHandlingBehavior<TRequest, TResponse> — catches unhandled handler exceptions and converts them to Result.Failure(Error.Exception(ex))
  • Registered as singleton between LoggingBehavior and CachingBehavior so callers always receive a typed failure instead of a raw exception
  • Zero overhead when TResponse is not Result/Result<T> (fast path skips wrapping entirely)
  • OperationCanceledException always propagates — never caught
  • Adds AddMediatorExceptionHandlingBehavior() selective registration method
  • 10 new tests (all passing); SKILL.md and API_REFERENCE.md updated with working code examples

Pipeline order (after this PR)

Position Behavior Lifetime
1 ValidationBehavior Scoped
2 LoggingBehavior Singleton
3 ExceptionHandlingBehavior ← new Singleton
4 CachingBehavior Singleton
5 TransactionScopeBehavior Singleton

Test plan

  • dotnet build — 0 errors, 0 warnings (all TFMs)
  • dotnet test --filter "FullyQualifiedName~Mediator" — 33/33 passed
  • Happy path (Result, Result) passes through unchanged
  • Exception → Result.Failure(Error.Exception(ex)) with correct code/description
  • OperationCanceledException propagates
  • Non-Result TResponse → exception propagates unchanged
  • Async handler throw (after await) caught correctly

🤖 Generated with Claude Code

Catches unhandled handler exceptions and converts them to Result.Failure
using Error.Exception(ex). Propagates OperationCanceledException always.
Zero overhead when TResponse is not Result/Result<T> (fast path skips
wrapping entirely). Registered as singleton after LoggingBehavior so
callers always receive a typed failure instead of a raw exception.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@senrecep senrecep merged commit 8ec6660 into main May 31, 2026
5 checks passed
@senrecep senrecep deleted the feat/mediator-exception-handling-behavior branch May 31, 2026 09:30
@codecov
Copy link
Copy Markdown

codecov Bot commented May 31, 2026

Codecov Report

❌ Patch coverage is 84.61538% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ls.Mediator/Behaviors/ExceptionHandlingBehavior.cs 91.42% 2 Missing and 1 partial ⚠️
...Mediator/DependencyInjection/MediatorExtensions.cs 25.00% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

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.

1 participant