ExceptionHandler¶
ExceptionHandler is a static class with a single method that maps SAIF.API.Exceptions.* exception types to fixed HTTP status codes. It deliberately re-throws its input inside a try block to dispatch on catch-clause type matching rather than is/pattern matching, so exception-to-status mapping stays centralized and consistent wherever a consuming controller or filter needs to translate a caught SAIF exception into an HTTP response. No call site exists inside this library; it is exposed as a static utility for host controllers/exception filters to call explicitly.*
Purpose¶
- Provides a single, centralized mapping from the four concrete
SAIF.API.Exceptions.SAIFExceptionsubtypes to their correspondingHttpStatusCode, and a safe500 InternalServerErrordefault for every unmapped exception type — see BR-WEBAPI-011.
Context¶
- Namespace:
SAIF.Web.API - Assembly/package:
SAIF.Web.API(NuGet) - Static class (no base type)
Public members¶
| Member | Signature | Purpose |
|---|---|---|
ToHttpError |
public static HttpStatusCode ToHttpError(Exception exception) |
Maps a caught exception to the HTTP status code a consumer should return: SAIF.API.Exceptions.InternalServerError → 500, NoMatchingDataException → 404, UnauthorizedAccessException (SAIF's own type, not System.UnauthorizedAccessException) → 403, InvalidDataException → 400, and any other exception (including a null argument, which raises a NullReferenceException caught by the bare catch) → 500 — see BR-WEBAPI-011. |
Behavioral notes¶
- The method re-throws its argument inside a
tryblock purely to use catch-clause type dispatch; this idiom unwinds and rewinds the call stack and overwrites the original stack trace recorded on the passed exception instance.* - Catch-clause ordering is not significant here because the four concrete SAIF exception types are sibling types; no clause shadows another (
src/SAIF.Web.API.Core/SAIFException.cs).
Constraints and usage notes¶
- All four concrete exception types and their common
SAIFExceptionbase are defined insrc/SAIF.Web.API.Core/SAIFException.cs, a separate project in this repository with no published documentation set in this run. - Passing
nulldoes not throw anArgumentNullException— the internal re-throw of anullreference raises aNullReferenceException, which the barecatchclause maps to500 InternalServerErrorlike any other unmapped exception.
Note: Statements marked with * were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts.
Sources¶
src/SAIF.Web.API/ExceptionHandler.cssrc/SAIF.Web.API.Core/SAIFException.cs