HTTP Status Code for External Dependency Error

rectangletangle picture rectangletangle · Aug 20, 2014 · Viewed 19.7k times · Source

What is the correct HTTP status code to return when a server is having issues communicating with an external API?

Say a client sends a valid request to my server A, A then queries server B's API in order to do something. However B's API is currently throwing 500's or is somehow unreachable, what status code should A return to the client? A 5* error doesn't seem correct because server A is functioning as it should, and a 4* error doesn't seem correct because the client is sending a valid request to A.

Answer

Aurélien picture Aurélien · Aug 20, 2014

Did you consider status codes 502 and 504?

502 – The server while acting as a gateway or a proxy, 
received an invalid response from the upstream server it accessed
in attempting to fulfill the request.

504 – The server, while acting as a gateway or proxy, 
did not receive a timely response from the upstream server 
specified by the URI (e.g. HTTP, FTP, LDAP) 
or some other auxiliary server (e.g. DNS) it needed to access 
in attempting to complete the request.

Of course, this would require a broad interpretation of "gateway" (implementation of interface A requiring a call to interface B), applied to the application layer. But this could be a nice way to say : "I cannot answer but it's not my fault nor yours".