1

首先我使用这个解决方案 - http://www.yiiframework.com/doc-2.0/guide-rest-error-handling.html 但是,我想自定义两种类型的错误。

  1. 当模型验证错误时。
  2. 当出现问题时(除外)

如果模型验证错误,我会得到响应,如下所示:

{
    "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 响应并且他们不知道错误或错误。

4

2 回答 2

0

也许这可以指导您如何更改错误格式:

class JsonErrors
    {
    public static function validation($model)
        {
                Yii::$app->response->statusCode = 422;
                //Yii::$app->response->format = 'json';
                $errorArray = [];
                foreach($model->getErrors() as $key => $val) {
                    $errorArray[] = [
                        'field' => $key,
                        'message' => implode(', ', $val) // $val is array (can contain multiple error messages)
                    ];
                }
                return $errorArray;
        }
    }

// 在控制器中:

return JsonErrors::validation($model);

// 配置:

'on beforeSend' => function ($event) {                
                $response = $event->sender;
                if($response->statusCode == 422)
                {
                    $details = $response->data;
                    if(isset($response->data["message"]) && is_string($response->data["message"])) $details = $response->data["message"];
                    if(isset($response->data["message"]) && is_array($response->data["message"])) $details = json_decode($response->data['message']);

                    $response->data = [
                        'message' => 'Please correct your data with correct values.',
                        'details' => $details
                    ];
                }
}
于 2020-06-09T09:37:37.713 回答
0

如果你使用默认的 yii/rest/Controller 模型并发送 422 错误。使用 yii/rest/Controller 或 yii/rest/ActiveController 或扩展它。或者使用自己的序列化器

http://www.yiiframework.com/doc-2.0/yii-rest-serializer.html

于 2017-03-30T08:07:20.580 回答