Skip to content

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 IRestResponse represents 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 package SAIF.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 from Content / ErrorMessage / ErrorException / status description; its nested ErrorMessage check is inverted relative to the enclosing guard, so the ErrorException.Message path is effectively unreachable and a transport failure with no Content yields a blank error message*.
  • ToKnownError harvests error text by scanning top-level JSON token paths for errormessage, exceptionmessage, message, or usermessage; a parse failure of content falls back to using the raw content as the message.
  • HTTP 404 maps to NoMatchingDataException; 401/403 map to UnauthorizedAccessException; 400 maps to InvalidDataException; every other status (including 0) maps to InternalServerError.

Constraints and usage notes

  • Both public members are static; there is no instance state to configure.
  • ToKnownError is public and may be called directly by consumers, not only via EnsureSuccessfulResponse.

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