Search code examples
httphttp-status-codes

What is the correct status code, if my server can't reach a third party server?


I have a server, which does a request to AWS S3.

What is the correct status code, if my server's request to S3 failed?


Let me break it down more.

  1. "Client" does request to "my server".
  2. "My server" does a request to a "third party server", which fails.
  3. Now "my server" needs to communicate to the "client", that the client's request failed because the request to the third party server failed.

I googled and found that 502 Bad Gateway, might be correct here, but I'm not sure because it also talks about my server being a proxy, which it isn't. My server does additional processing and database calls with the content of the request before the call to AWS S3, as well as after it got the result from AWS.


Solution

  • This is from my experience:

    Let's consider your scenario:

    • we have ServiceA and ServiceB
    • the client calls ServiceA
    • then ServiceA calls ServiceB
    • ServiceA returns the result of calling ServiceB to the client

    Considering your ServiceA actually does some business logic instead of simply calling ServiceB (like a proxy), I think it is better to response with 200 OK to the client and wrap the actual results of calling ServiceB to response payload.

    For example, like this:

    {
      "serviceBresponse": {
        "status": 503,
        ... another information 
      }
    }
    

    Semantically it means the following:

    Is your ServiceA respondable? Yes, it is. Is your ServiceA was able to process the client request? Yes, it was. What ServiceB responded? See the payload.

    Since with this approach you can difer from client perspective which service is actually responded with non-200 status. If ServiceA responded with non-200 status, then the ServiceA was not able to process the request. If ServiceA responded with 200, then you can be sure that ServiceA is okay.