ASP.NET Core Web API Interview Question #195
How should a Web API expose errors from downstream services?
Senior API Design, Architecture, Scalability & Production Scenarios Senior Advanced
Quick Interview Answer
Do not leak the downstream provider's internal exception, URL, credentials, or raw body. Translate failures into your API's documented error model based on what the failure means to your client.
Detailed Explanation
Do not leak the downstream provider's internal exception, URL, credentials, or raw body. Translate failures into your API's documented error model based on what the failure means to your client.
For example, an unavailable required dependency may result in a temporary server-side failure, while a known downstream business rejection may map to a documented 4xx outcome. Preserve diagnostic details in secure logs and traces.
Code Example
catch (PaymentServiceUnavailableException ex)
{
_logger.LogError(ex,
"Payment dependency unavailable for order {OrderId}", orderId);
return Problem(
statusCode: StatusCodes.Status503ServiceUnavailable,
title: "Payment service temporarily unavailable");
}