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

 ANANREMEMBERO38_810 发布于 2022-12-26 09:40

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

我的服务可以查询第三方API.偶尔,我会收到一条500错误消息..要么是因为过期的令牌,要么是等等.

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

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

更新:下面是我从第三方看到的.

我想我喜欢...的想法,503 Service unavailable错误信息告诉用户什么是错的,以及如何解决它.

更新显示第三方的回复:

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',
);

Dawid Ferenc.. 7

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

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

只有在您的API充当代理而没有任何其他功能的情况下,我才会将第三方API的HTTP代码镜像到用户.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有