1

For first I use this solution - http://www.yiiframework.com/doc-2.0/guide-rest-error-handling.html But, I want to customize two types error.

  1. When model validation wrong.
  2. When something wrong (Exceptiin)

If model validation wrong i get response, like that:

{
    "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."
        }
    ]
}

But i want like that:

{
    "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."
        }
]

    ]
}

Second type error i get

{
    "success": false,
    "data": {
        "name": "Exception",
        "message": "Invalid request arguments",
        "code": 0,
        "type": "yii\\base\\InvalidParamException",       
        ]
    }
}

But i Want:

{
    "success": false,
    "data": {
        "errors" : 1, <---------------- 
        "name": "Exception",
        "message": "Invalid request arguments",
        "code": 0,
        "type": "yii\\base\\InvalidParamException",       
        ]
    }
}

Because in anyway user get 200 Response and they don know about error or mistake.

2 Answers2

0

If you are using default yii/rest/Controller model with error send 422. Use yii/rest/Controller or yii/rest/ActiveController or extend it. Or use own Serializer

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

zakrzu
  • 465
  • 4
  • 9
0

Maybe this could guide you, how to change error format:

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;
        }
    }

// in controller:

return JsonErrors::validation($model);

// config:

'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
                    ];
                }
}
TomoMiha
  • 898
  • 1
  • 11
  • 11