ExceptionHandler¶
ExceptionHandler is a stateless static component that guards HTTP response success and translates failing responses into typed exceptions from SAIF.Web.API.Core's SAIF.API.Exceptions.* hierarchy, so consumers of JsonHttpClient catch domain exceptions rather than transport or RestSharp types.
Purpose¶
- Decide whether a completed
IRestResponserepresents success or failure, and short-circuit (return normally) for success (BR-CLI-006). - Select the most informative available error text from a failing response, preferring the API's own error payload (BR-CLI-008, BR-CLI-017).
- Map an HTTP status code to a specific
SAIF.API.Exceptions.*type and throw it (BR-CLI-007).
Context¶
- Namespace:
SAIF.Web.API.Clients - Assembly/package:
SAIF.Web.API.Clients(NuGet packageSAIF.Web.API.Clients)
Public members¶
| Member | Signature | Purpose |
|---|---|---|
EnsureSuccessfulResponse |
static void EnsureSuccessfulResponse(IRestResponse response) |
Throws a mapped SAIFException when the response status is < 200 or >= 400; returns normally otherwise. 3xx redirects pass as success. See EnsureSuccessfulResponse. |
ToKnownError |
static void ToKnownError(HttpStatusCode status, string content) |
Extracts an error message from the payload and throws a typed SAIF.API.Exceptions.* exception; never returns normally. See ToKnownError. |
Behavioral notes¶
GetContent(IRestResponse response)(private) selects the best available error text fromContent/ErrorMessage/ErrorException/ status description; its nestedErrorMessagecheck is inverted relative to the enclosing guard, so theErrorException.Messagepath is effectively unreachable and a transport failure with noContentyields a blank error message*.ToKnownErrorharvests error text by scanning top-level JSON token paths forerrormessage,exceptionmessage,message, orusermessage; a parse failure ofcontentfalls back to using the raw content as the message.- HTTP
404maps toNoMatchingDataException;401/403map toUnauthorizedAccessException;400maps toInvalidDataException; every other status (including0) maps toInternalServerError.
Constraints and usage notes¶
- Both public members are
static; there is no instance state to configure. ToKnownErroris public and may be called directly by consumers, not only viaEnsureSuccessfulResponse.
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.Clients/ExceptionHandler.cs