Yii2 rest api exception

I am using api module in which all my controllers are active controller . When my result is ok then my outpul will be:

{ "success": true, "data": { "responsecode": true, "response": "Successfully Updated Records" } }

But something went wrong it show me a object(yii\base\ErrorException). But i need the response like above format error message should be in under the above format. I also tried in many way but my problem not fixed . In my config main i’m format the response and my controller is active controller.

'response' => [ 'class' => 'yii\web\Response', 'on beforeSend' => function ($event) { $response = $event->sender; if ($response->data !== null) { $response->data = [ 'success' => $response->isSuccessful, 'data' => $response->data, ]; $response->statusCode = 200; } }

Please help how to set the response in a genric format.