Yii2 REST:如何自定义错误响应?

时间:2017-03-28 10:41:10

标签: rest yii2 yii2-advanced-app yii2-validation

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

  1. 模型验证错误时。
  2. 出错时(Exceptiin)
  3. 如果模型验证错误,我会得到响应,如:

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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
                    ];
                }
}