Littlebob
Littlebob

Reputation: 153

Yii2 Rest Api customize error response

I'm working on a REST API using Yii2 and I'm trying to customize the error response. By default, if I use the wrong credentials when submitting a request I see this:

{
    "name": "Unauthorized",
    "message": "You are requesting with an invalid credential.",
    "code": 0,
    "status": 401
}

Where and how can I remove the code and name lines?

Upvotes: 2

Views: 2081

Answers (2)

Edward Nguyen
Edward Nguyen

Reputation: 1

I have new solution:

return [
    // ...
    'components' => [
        'response' => [
            'format' => yii\web\Response::FORMAT_JSON,
            'charset' => 'UTF-8',
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;

                // exception with http code != 200 and 302
                if ($response->statusCode !== 200 && $response->statusCode !== 302) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'message' => YII_DEBUG ?$response->data: 'System error'
                    ];
                    $response->statusCode = 200;
                }
                // normal response
                else{
                    // both case: controller implement Controller or ActiveController
                    $result =  [
                        'success' => isset($response->data['success'])?$response->data['success']: true
                    ];

                   // check message is set
                if (isset($response->data['message'])){
                    $result = array_merge($result, ['message' => $response->data['message']]);
                }
                else{
                    // check data is set
                    // both case: controller implement Controller or ActiveController
                    if (isset($response->data['data']))
                        $result = array_merge($result, ['data' => $response->data['data']]);
                    else{
                        $result = array_merge($result, ['data' => $response->data]);
                    }
                }

                    $response->data = $result;
                    $response->statusCode = 200;
                }
            },
        ]
    ],
];

Upvotes: 0

Bloodhound
Bloodhound

Reputation: 2966

try this code in your application configuration.

return [
    // ...
    'components' => [
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                if ($response->data !== null && Yii::$app->request->get('suppress_response_code')) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'data' => $response->data,
                    ];
                    $response->statusCode = 200;
                }
            },
        ],
    ],
];

For more Details

Upvotes: 2

Related Questions