1

I want to customize this following error message

Error 404: Not Found
{
    "name": "Not Found",
    "message": "Object not found: 6",
    "code": 0,
    "status": 404,
    "type": "yii\\web\\NotFoundHttpException"
}

to:

Error 404: Not Found
{
    "name": "Not Found",
    "message": "Country not found for: 6",
    "code": 404,
    "status": Error
}

Where is need to write this customization code?

4
  • throw your own exceptions maybe and write w.e. in message Commented Jun 13, 2016 at 7:55
  • exactly, something like this... throw new NotFoundHttpException("Country not found for: ". $country_id); Commented Jun 13, 2016 at 9:29
  • But im using basic rest calls ..Idont have any functions,my controller is ` class CountryController extends ActiveController { public $modelClass = 'app\models\Country'; }` Commented Jun 13, 2016 at 9:53
  • 1
    yii2-framework.readthedocs.io/en/latest/guide/… yiiframework.com/doc-2.0/guide-rest-error-handling.html Commented Dec 19, 2016 at 5:18

3 Answers 3

2

try this (e.g. config/web.php):

return [
    // ...
    'components' => [
        // ...
        'response' => [
            // ...
            'on beforeSend' => function (yii\base\Event $event) {
                $response = $event->sender;
                if (404 === $response->statusCode && is_array($response->data)) {
                    $response->data['code'] = $response->data['status'];
                    $response->data['status'] = 'Error';
                    unset($response->data['type']);
                }
            },
        ],

Sign up to request clarification or add additional context in comments.

Comments

1

You need to override errorHandler which extends yii\web\ErrorHandler, just convertExceptionToArray method

/**
 * Converts an exception into an array.
 * @param \Exception|\Error $exception the exception being converted
 * @return array the array representation of the exception.
 */
protected function convertExceptionToArray($exception)
{
    if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) {
        $exception = new HttpException(500, Yii::t('yii', 'An internal server error occurred.'));
    }

    $array = [
        'name' => ($exception instanceof Exception || $exception instanceof ErrorException) ? $exception->getName() : 'Exception',
        'message' => $exception->getMessage(),
        'code' => $exception->getCode(),
    ];
    if ($exception instanceof HttpException) {
        $array['status'] = $exception->statusCode;
    }
    if (YII_DEBUG) {
        $array['type'] = get_class($exception);
        if (!$exception instanceof UserException) {
            $array['file'] = $exception->getFile();
            $array['line'] = $exception->getLine();
            $array['stack-trace'] = explode("\n", $exception->getTraceAsString());
            if ($exception instanceof \yii\db\Exception) {
                $array['error-info'] = $exception->errorInfo;
            }
        }
    }
    if (($prev = $exception->getPrevious()) !== null) {
        $array['previous'] = $this->convertExceptionToArray($prev);
    }

    return $array;
}

The code above is from Yii, you just need to tweak it a little. Then add it to config : ( usually web.php)

    'errorHandler' => [
        'class' => 'your\namespace\YourErrHandler',
    ],

Comments

0

Im not sure what I do is right or wrong but it works. Just create a new custom php file called ErrorMsg.php

<?php
use Yii;
use yii\web\HttpException;
class ErrorMsg extends \Exception
{
    
    public static function customErrorMsg($error_code,$message = null, $code = 0, \Exception $previous = null,$extra_content=NULL)
    {
        $httpException = new HttpException($error_code,$message,$code,$previous);
        Yii::$app->response->statusCode = $error_code;
        $custom_err = array(
                            'name'=> $httpException->getName(),
                            'message' => $message,
                            'code' => $code,
                            'extraContent' => $content,
                            'status' => $error_code
        );
        return $custom_err;
    }

and call the functions wherever you want. Example

return ErrorMsg::customErrorMsg(400,"Message Here",11,NULL,"Extra Content Here");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.