Search code examples
c#http

Should I return 502 if my API calls another API and the last one fails?


Is it correct to return 502 in the case a call to an API fails even if my API is not acting totally as a proxy, but rather as a client? Code example:

public async Task<IActionResult> CheckSomething(MyModel model)
{
    try
    {
        var result = await _anotherApi.CheckSmt();
        if (!result.Successful)
        {
            return StatusCode(StatusCodes.Status502BadGateway, $"Error on external API");
        }

        return Ok(result.Result);
    }
    catch (Exception ex)
    {

    }
}

Solution

  • No, there is a good code for that, HTTP status code 503 Service Unavailable. This code indicates that the server is currently unable to handle the request due to a temporary overloading or maintenance of the server.

    A temporary problem may be a 3rd party service, remember that you can always return text within the code so you can explain a bit more the causes as you mentioned

    Error on external API

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503