ASP.NET Core Web API Interview Question #163

How should an API handle outbound HTTP failures?

Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced

Quick Interview Answer

Distinguish cancellation, timeout, transient transport failure, expected downstream 4xx responses, and server-side 5xx responses. Do not convert every failure into an identical exception or retry every status automatically.

Detailed Explanation

Distinguish cancellation, timeout, transient transport failure, expected downstream 4xx responses, and server-side 5xx responses. Do not convert every failure into an identical exception or retry every status automatically.

Log enough context to diagnose the dependency while avoiding credentials and sensitive payloads. Translate downstream failures into an appropriate application outcome for your own API contract.

Code Example

var response = await _http.GetAsync(
    $"products/{id}", token);

if (response.StatusCode == HttpStatusCode.NotFound)
    return null;

response.EnsureSuccessStatusCode();