I have an API that, upon a client's POST request:
- Parse the client's request
- Do stuff with the data in the request
- Send a request to a remote server
- Receives a response from the remote server
- Do more stuff with the data in the response
- Persist the data in a database
- Send a response to the client
In other words, normal web server stuff.
If the remote server sends me a 500
(or any other error), should I send my client a 500 Internal Server Error
or a 502 Bad Gateway
?
From the RFC 7231,
6.6.3. 502 Bad Gateway
The 502 (Bad Gateway) status code indicates that the server, while
acting as a gateway or proxy, received an invalid response from an
inbound server it accessed while attempting to fulfill the request.
In one hand, the 502 Bad Gateway
looks like this is intended for "dumb" servers that just forwards the request to the remote server, and returns its response to the client, without much processing.
In the other hand, to not be able to access the remote server doesn't sound like a 500 Internal Server Error
... more like a "Remote Server Error".
Which one should I use? 500
, 502
or other?