EnsureSuccessfulResponse (ExceptionHandler)¶
ExceptionHandler.EnsureSuccessfulResponse is the guard that decides whether a completed HTTP response should be converted into a typed exception, and — if so — selects the error text to carry.
| Field | Value |
|---|---|
| Operation ID | OP-08 |
| Trigger | GetJsonResponse step 11. |
| Inputs | IRestResponse response. |
| Outputs | None (void), or a thrown SAIFException. |
| Primary components | ExceptionHandler.EnsureSuccessfulResponse(IRestResponse) |
| Entry point | src/SAIF.Web.API.Clients/ExceptionHandler.cs |
Steps¶
- Cast
response.StatusCodetoint. - Transport failure gives
0. - If
status < 200orstatus >= 400: proceed. Otherwise return normally (BR-CLI-006). - 3xx redirect statuses are treated as success and pass the guard, since only
< 200and>= 400fail. Status0fails the guard (0 < 200). - Select the error text via
GetContent(response)(BR-CLI-017). GetContentreturnsresponse.Contentwhen non-blank; otherwise — whenErrorMessageis non-blank orErrorExceptionis non-null — it enters a nested branch whose first test isif (response.ErrorMessage.IsNullOrWhiteSpace()) return response.ErrorMessage;, which can only return a blank string; otherwise, for non-BadRequeststatuses, it returns"{code} - {statusDescription}"; otherwisestring.Empty*.- The nested
ErrorMessagetest is inverted relative to its enclosing guard, so theErrorException.Messagepath is effectively unreachable and a transport-level failure with noContentyields a blank error message*. - Throw the mapped exception.
- Embedded logic: parse the content for a known message field, then switch on the status code to select the exception type.
Origin: This step is implemented in ToKnownError (
SAIF.Web.API.Clients) and executed on behalf of this operation. - Never returns.
Process flow¶
flowchart TD
Start(["EnsureSuccessfulResponse<br>called"]) --> Cast[Cast StatusCode to int]
Cast --> Range{"status < 200 or >=<br>400?"}
Range -->|No| EndOk([Return normally])
Range -->|Yes| Content["GetContent: select error<br>text"]
Content --> Throw(["ToKnownError: throw<br>mapped SAIFException"])
Failure and edge-case behavior¶
- A 3xx redirect status passes the guard as success even though it is not a completed 2xx response.
- The inverted inner condition in
GetContentmakes theErrorException.Messagepath unreachable, so a transport-level failure with noContentproduces a blank exception message*.
Related documentation¶
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