当依赖项/下游(例如第三方API)失败时要使用什么HTTP状态代码? [英] What HTTP Status code to use when a dependency/downstream (like a 3rd party API) fails?
问题描述
我们的API中有一条路由(当被调用时)到达了另一个第三方API.
We have a route in our API which (when called) hits another 3rd party API.
例如
HTTP-GET /account/1
这会从我们的数据库以及从..例如..第三方API(例如Auth0/Okta/SalesForce/其他)中返回一些数据.
this returns some data from our database AND from .. say .. a 3rd party api like Auth0/Okta/SalesForce/whatever.
现在,如果此第三方api调用由于任何原因失败(失败== 4 **,5 **或什至200 OK,但内容是一些错误消息),那么我应该将什么错误状态代码传递回给客户调用 my API?
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?
最初,我在想一个HTTP-500-Server-Error
,但是...现在我不太确定,因为如果我将错误消息返回给服务器时,我可以防止该错误在服务器中发生.客户.因此,我想到了,返回一个HTTP-200-OK
,其中包含下游问题/错误的某些键/值...但这确实是 确定的(双关语).对我来说,HTTP-200-OK
就像返回的答案真的是 一样.
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.
我觉得HTTP-500
是针对已发生但尚未真正处理和/或解决的错误.
I feel like an HTTP-500
is for errors that occur but haven't really been handled and/or accounted for.
推荐答案
现在,如果此第三方api调用由于任何原因失败(失败== 4 **,5 **或什至200 OK,但内容是一些错误消息),那么我应该将什么错误状态代码传递回给客户调用 my API?
API应该从消费者的角度进行设计.在大多数情况下,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.
如果对下游服务器的请求阻止您的服务器满足客户端请求,则可以进行 503
.或者,您可以返回一些缓存的数据(如果有的话),然后返回 2xx
状态代码.
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.
这篇关于当依赖项/下游(例如第三方API)失败时要使用什么HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!