外部依赖性错误的HTTP状态代码 [英] HTTP Status Code for External Dependency Error

查看:327
本文介绍了外部依赖性错误的HTTP状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当服务器与外部API通信时出现问题时返回的正确HTTP状态代码是什么?

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

假设客户端向我的服务器发送有效请求A然后,A查询服务器B的API以执行某些操作。但是B的API目前正在抛出500或者某种程度上无法访问,应该返回客户端的状态代码是什么? 5 *错误似乎不正确,因为服务器A正常运行,并且4 *错误似乎不正确,因为客户端正在向A发送有效请求。

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.

推荐答案

由于API依赖于某些不可用的东西,因此其服务也不可用。

Since the API relies on something that is not available, its service is unavailable as well.

我认为状态代码 503:服务不可用最适合您的情况。来自 RFC说明

I would think that the status code 503: Service Unavailable is the best fit for your situation. From the RFC description:


由于服务器临时过载或维护,服务器当前无法处理请求。这意味着这是一个暂时的条件,经过一段时间的延迟后会得到缓解。如果已知,延迟的长度可以在Retry-After报头中指示。如果没有给出Retry-After,客户端应该像处理500响应一样处理响应。

The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header. If no Retry-After is given, the client SHOULD handle the response as it would for a 500 response.

当然,描述意味着这个状态代码应该应用于服务器本身的错误(而不是表示外部依赖的问题)。但是,这是RFC状态代码中的最佳选择,我不建议使用任何自定义状态代码,以便任何人都能理解它们。

Granted, the description implies that this status code should be applied for errors on the server itself (and not to signal a problem with an external dependency). However, this is the best fit within the RFC status codes, and I wouldn't suggest using any custom status codes so anyone can understand them.

或者,如果您的API支持一种沟通错误的方式(例如,告诉用户他提供的ID不正确)您可以使用此方法告诉用户依赖项不可用。这可能会更加友好,可能会避免在用户方面进行一些错误搜索,因为至少有些用户不会熟悉除403,404和500之外的任何状态代码,具体取决于您的受众。

Alternatively, if your API supports a way of communicating errors (e.g. to tell the user that the ID he supplied is incorrect) you may be able to use this method to tell the user that the dependency is unavailable. This might be a little friendlier and might avoid some bug searching on the user's side, since at least some of the users won't be familiar with any status codes besides 403, 404 and maybe 500, depending on your audience.

这篇关于外部依赖性错误的HTTP状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆