Map a SAIF exception to an HTTP status code¶
ExceptionHandler.ToHttpError(Exception) (public static) provides a reusable, deliberate catch-clause type dispatch from SAIF.API.Exceptions.* to HttpStatusCode.
| Field | Value |
|---|---|
| Operation ID | OP-06 |
| Trigger | Called explicitly by a consumer (no call site exists inside this library). |
| Inputs | Exception |
| Outputs | HttpStatusCode |
| Primary components | ExceptionHandler.ToHttpError(Exception) |
| Entry point | src/SAIF.Web.API/ExceptionHandler.cs |
Steps¶
throw exception;inside atryblock — the method deliberately re-throws to use catch-clause type dispatch rather thanis/pattern matching (BR-WEBAPI-011).- The idiom unwinds/rewinds the stack and overwrites the original stack trace on the passed instance.*
catch (SAIF.API.Exceptions.InternalServerError)→500 InternalServerError(BR-WEBAPI-011).catch (SAIF.API.Exceptions.NoMatchingDataException)→404 NotFound(BR-WEBAPI-011).catch (SAIF.API.Exceptions.UnauthorizedAccessException)→403 Forbidden(BR-WEBAPI-011).- SAIF's own type, not
System.UnauthorizedAccessException. catch (SAIF.API.Exceptions.InvalidDataException)→400 BadRequest(BR-WEBAPI-011).- Bare
catch→500 InternalServerError(BR-WEBAPI-011). - Default for every unmapped exception, including a
nullargument (throw nullraises aNullReferenceExceptioncaught here).*
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).
Process flow¶
flowchart TD
subgraph MainFlow["Catch-clause dispatch<br>path"]
Start(["Consumer calls<br>ToHttpError"]) --> Try["throw exception inside<br>try"]
Try --> Internal{InternalServerError?}
Internal -->|No| NoData{NoMatchingDataException?}
NoData -->|No| Unauth{"UnauthorizedAccess<br>Exception?"}
Unauth -->|No| Invalid{InvalidDataException?}
end
Internal -->|Yes| S500a[500 InternalServerError]
NoData -->|Yes| S404[404 NotFound]
Unauth -->|Yes| S403[403 Forbidden]
Invalid -->|Yes| S400[400 BadRequest]
Invalid -->|No| S500b["500 InternalServerError<br>(bare catch)"]
Failure and edge-case behavior¶
- A
nullexception argument raisesNullReferenceExceptionat thethrow, which is caught by the barecatchand mapped to500.*
Note: Statements marked with * were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts.
Related documentation¶
Sources¶
src/SAIF.Web.API/ExceptionHandler.cssrc/SAIF.Web.API.Core/SAIFException.cs