rest - 第 3 方 API 返回 500 错误,我的 API 应返回什么代码

标签 rest http-status-codes laminas-api-tools

我在基于 ZF2(Zend Framework 2)的框架中编写了一个 API,称为 Apigility。

我的服务可以查询第 3 方 API。偶尔,我会收到一条 500 错误消息..要么是由于 token 过期,要么是诸如此类。

我的 API 应该如何回应我的客户?

我一开始以为我应该返回 500,但实际上这似乎是错误的。我不想返回一个错误,表明我已经崩溃了......它是 3rd 方有 500'd。

更新:以下是我从第三方看到的。

我想我喜欢 503 Service unavailable 的想法.. 一条错误消息提示用户出了什么问题,以及如何修复它。

更新显示第 3 方的回应:

Error performing request to OAuth Provider. 
HTTP/1.1 500 Internal Server Error
Server: nginx/1.1.19
Date: Fri, 22 Aug 2014 20:24:40 GMT
Content-Type: text/html
Content-Length: 20
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.1
Set-Cookie: lang_select_language=en; Expires=Sun, 21-Aug-2016 20:24:42 GMT; Path=/
X-WI-SRV: FR-EQX-WEB-03
Vary: Accept-Encoding
Content-Encoding: gzip

想法?
/**
 * Status titles for common problems
 *
 * @var array
 */
protected $problemStatusTitles = array(
    // CLIENT ERROR
    400 => 'Bad Request',
    401 => 'Unauthorized',
    402 => 'Payment Required',
    403 => 'Forbidden',
    404 => 'Not Found',
    405 => 'Method Not Allowed',
    406 => 'Not Acceptable',
    407 => 'Proxy Authentication Required',
    408 => 'Request Time-out',
    409 => 'Conflict',
    410 => 'Gone',
    411 => 'Length Required',
    412 => 'Precondition Failed',
    413 => 'Request Entity Too Large',
    414 => 'Request-URI Too Large',
    415 => 'Unsupported Media Type',
    416 => 'Requested range not satisfiable',
    417 => 'Expectation Failed',
    418 => 'I\'m a teapot',
    422 => 'Unprocessable Entity',
    423 => 'Locked',
    424 => 'Failed Dependency',
    425 => 'Unordered Collection',
    426 => 'Upgrade Required',
    428 => 'Precondition Required',
    429 => 'Too Many Requests',
    431 => 'Request Header Fields Too Large',
    // SERVER ERROR
    500 => 'Internal Server Error',
    501 => 'Not Implemented',
    502 => 'Bad Gateway',
    503 => 'Service Unavailable',
    504 => 'Gateway Time-out',
    505 => 'HTTP Version not supported',
    506 => 'Variant Also Negotiates',
    507 => 'Insufficient Storage',
    508 => 'Loop Detected',
    511 => 'Network Authentication Required',
);

最佳答案

好吧,我认为这取决于您,您将使用哪个错误代码。但是如果您的 API 的实际功能取决于第三方 API,我会考虑使用 HTTP 代码 503 Service Unavailable ,因为您的服务将不可用,直到 3rd 方 API 无法工作,无论 3rd 方 API 返回什么 HTTP 代码。我还会在响应负载中包含一些详细信息(错误消息)。

或者您可以返回 HTTP 代码 200 OK 并将自定义错误代码和消息作为响应负载发送,当然,因为对您的 API 的 HTTP 请求实际上是成功的。但我更愿意使用 HTTP 代码来指示 API 端点的状态。

只有在您的 API 充当代理而没有任何附加功能的情况下,我才会将来自 3rd 方 API 的 HTTP 代码镜像给用户。

关于rest - 第 3 方 API 返回 500 错误,我的 API 应返回什么代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25454997/

相关文章:

php - 在 Woocommerce REST API 中按类别获取产品

java - 登录 - 填写表格并提交

rest - 请求处理不成功的 HTTP 响应

doctrine-orm - 如何在带有 Doctrine 的 Apigility 应用程序中同时使用多个版本?

rest - Thingsboard:在仪表板中创建一个 "rest api call"按钮

java - 没有注释的 Restful Web 服务?

c# - Office 365 API 身份验证表单 REST API

mysql - 使用 CURL 和 shell 脚本验证 mysql 数据库中 URL 的状态

php - 如何从这段代码zf2中获取查询sql指令?

rest - 使用 Postman 时 Apigility 中的基本身份验证