如何在 Apigility 中扩展文档中的错误代码?
How to expand error codes in documentation in Apigility?
我没有找到描述所有响应代码的方式,我只看到默认代码。我有很多回复,想描述一下。我也有兴趣描述导致 400 响应错误的不同请求(例如,具有此类数据的请求将 return 该消息等等),是否应该在 API 文档中描述?
如何在 Apigility 中自定义错误响应
Apigility 与 ZF Api Problem 无缝协作以进行错误处理。
从控制器或侦听器创建错误响应非常简单:
use ZF\ApiProblem\ApiProblem;
...
return new ApiProblem(500, "My Internal Server Error");
常见错误的状态标题设置为here in the class。
建议使用适合出现的问题的有效 http error codes,但您当然可以自定义标题。您可以直接从您的控制器和侦听器 return 您的 ApiProblem
,Apigility
将正确处理 error
和 return 渲染的 json
响应 Content-Type
headers 设置为 application/problem+json
.
不确定这是否仍然是现实,但您可以使用 Api 响应对象中的构造函数参数 additional
来自定义您的答案;
return new ApiProblem(
Response::STATUS_CODE_500,
'Internal Server Error',
null,
'This is the title',
[
'specific' => 'entry',
'more_problem_details' => [
'custom_code' => 'custom_code_value',
'custom_message' => 'custom_message_text',
],
]
);
响应将如下所示:
{
"specific": "entry",
"more_problem_details": {
"custom_code": "custom_code_value",
"custom_message": "custom_message_text"
},
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "This is the title",
"status": 500,
"detail": "Internal Server Error"
}
检查此以获取更多详细信息:
https://docs.zendframework.com/zend-problem-details/response/
我没有找到描述所有响应代码的方式,我只看到默认代码。我有很多回复,想描述一下。我也有兴趣描述导致 400 响应错误的不同请求(例如,具有此类数据的请求将 return 该消息等等),是否应该在 API 文档中描述?
如何在 Apigility 中自定义错误响应
Apigility 与 ZF Api Problem 无缝协作以进行错误处理。
从控制器或侦听器创建错误响应非常简单:
use ZF\ApiProblem\ApiProblem;
...
return new ApiProblem(500, "My Internal Server Error");
常见错误的状态标题设置为here in the class。
建议使用适合出现的问题的有效 http error codes,但您当然可以自定义标题。您可以直接从您的控制器和侦听器 return 您的 ApiProblem
,Apigility
将正确处理 error
和 return 渲染的 json
响应 Content-Type
headers 设置为 application/problem+json
.
不确定这是否仍然是现实,但您可以使用 Api 响应对象中的构造函数参数 additional
来自定义您的答案;
return new ApiProblem(
Response::STATUS_CODE_500,
'Internal Server Error',
null,
'This is the title',
[
'specific' => 'entry',
'more_problem_details' => [
'custom_code' => 'custom_code_value',
'custom_message' => 'custom_message_text',
],
]
);
响应将如下所示:
{
"specific": "entry",
"more_problem_details": {
"custom_code": "custom_code_value",
"custom_message": "custom_message_text"
},
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "This is the title",
"status": 500,
"detail": "Internal Server Error"
}
检查此以获取更多详细信息:
https://docs.zendframework.com/zend-problem-details/response/