首先我使用这个解决方案 - http://www.yiiframework.com/doc-2.0/guide-rest-error-handling.html 但是,我想自定义两种类型的错误。
- 当模型验证错误时。
- 当出现问题时(除外)
如果模型验证错误,我会得到响应,如下所示:
{
"success": false,
"data": [
{
"field": "country_id",
"message": "Country Id cannot be blank."
},
{
"field": "currency_id",
"message": "Currency Id cannot be blank."
},
{
"field": "originator_id",
"message": "Originator Id cannot be blank."
}
]
}
但我想要这样:
{
"success": false,
"data": [
"errors": [
{
"field": "country_id",
"message": "Country Id cannot be blank."
},
{
"field": "currency_id",
"message": "Currency Id cannot be blank."
},
{
"field": "originator_id",
"message": "Originator Id cannot be blank."
}
]
]
}
我得到的第二种错误
{
"success": false,
"data": {
"name": "Exception",
"message": "Invalid request arguments",
"code": 0,
"type": "yii\\base\\InvalidParamException",
]
}
}
但我想要:
{
"success": false,
"data": {
"errors" : 1, <----------------
"name": "Exception",
"message": "Invalid request arguments",
"code": 0,
"type": "yii\\base\\InvalidParamException",
]
}
}
因为无论如何用户都会得到 200 响应并且他们不知道错误或错误。