What HTTP Status code to use when a dependency/downstream (like a 3rd party API) fails?

六月ゝ 毕业季﹏ 提交于 2019-12-31 03:04:20

问题


We have a route in our API which (when called) hits another 3rd party API.

e.g.

HTTP-GET /account/1

this returns some data from our database AND from .. say .. a 3rd party api like Auth0/Okta/SalesForce/whatever.

Now, if this 3rd party api call fails for any reason (fails == 4**, 5** or even a 200 OK but the content is some error message) then what error status code should I pass back to the client calling my API?

Initially I was thinking an HTTP-500-Server-Error but ... I'm not so sure now because I can prevent this error from occurring in the server if I return a nice error message back to the client. So then I thought, return an HTTP-200-OK which contains some key/value of the downstream issue/error ... but is this really OK (pun intended). To me, HTTP-200-OK is like the answer returned is really ok.

So i'm not sure what people do in this scenario.

I feel like an HTTP-500 is for errors that occur but haven't really been handled and/or accounted for.


回答1:


Now, if this 3rd party api call fails for any reason (fails == 4**, 5** or even a 200 OK but the content is some error message) then what error status code should I pass back to the client calling my API?

APIs should be designed from the consumer's perspective. In most of situations, the API consumer wont't care if the request is fulfilled by the server they are firing a request to or by a downstream server.

If the request to the downstream server prevents your server from fulfilling the client request, you could go for 500 or 503. Alternatively you could return some cached data (if you have any) and return a 2xx status code.



来源:https://stackoverflow.com/questions/52566434/what-http-status-code-to-use-when-a-dependency-downstream-like-a-3rd-party-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!