---
title: ExceptionHandler
summary: API reference for ExceptionHandler.
status: draft
owner: andmil
last_reviewed: 2026-09-15
library: SAIF.Web.API
reference_type: package
---

# 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.SAIFException` subtypes to their corresponding `HttpStatusCode`, and a safe `500 InternalServerError` default for every unmapped exception type — see [BR-WEBAPI-011](../../business-rules.md#br-webapi-011-saif-exception-types-map-to-fixed-http-status-codes).

## 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](../../business-rules.md#br-webapi-011-saif-exception-types-map-to-fixed-http-status-codes). |

## Behavioral notes
- The method re-throws its argument inside a `try` block 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 `SAIFException` base are defined in `src/SAIF.Web.API.Core/SAIFException.cs`, a separate project in this repository with no published documentation set in this run.
- Passing `null` does not throw an `ArgumentNullException` — the internal re-throw of a `null` reference raises a `NullReferenceException`, which the bare `catch` clause maps to `500 InternalServerError` like 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.cs`
- `src/SAIF.Web.API.Core/SAIFException.cs`
