yii includes a built-in error handler. the yii error handler does the following −
- converts all non-fatal php errors into catchable exceptions.
 - displays all errors and exceptions with a detailed call stack.
 - supports different error formats.
 - supports using a controller action to display errors.
 
to disable the error handler, you should define the yii_enable_error_handler constant to be false in the entry script. the error handler is registered as an application component.
step 1 − you can configure it in the following way.
return [
   'components' => [
      'errorhandler' => [
         'maxsourcelines' => 10,
      ],
   ],
];
the above configuration sets the number of source code lines to be displayed to 10. the error handler converts all non-fatal php errors into catchable exceptions.
step 2 − add a new function called actionshowerror() to the sitecontroller.
public function actionshowerror() {
   try {
      5/0;
   } catch (errorexception $e) {
      yii::warning("ooops...division by zero.");
   }
   // execution continues...
}
step 3 − go to the url http://localhost:8080/index.php?r=site/show-error. you will see a warning message.
if you want to show the user that his request is invalid, you may throw the yii\web\notfoundhttpexception.
step 4 − modify the actionshowerror() function.
public function actionshowerror() {
   throw new notfoundhttpexception("something unexpected happened");
}
step 5 − type the address http://localhost:8080/index.php?r=site/show-error in the address bar. you will see the following http error.
when the yii_debug constant is true, the error handler will display errors with a detailed call stack. when the constant is false, only the error message will be displayed. by default, the error handler shows errors using these views −
@yii/views/errorhandler/exception.php − the view file is used when errors should be displayed with call stack information.
@yii/views/errorhandler/error.php − the view file is used when errors should be displayed without call stack information.
you can use dedicated error actions to customize the error display.
step 6 − modify the errorhandler application component in the config/web.php file.
<?php
   $params = require(__dir__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basepath' => dirname(__dir__),
      'bootstrap' => ['log'],
      'components' => [
         'request' => [
            // !!! insert a secret key in the following (if it is empty) - this
               //is required by cookie validation
            'cookievalidationkey' => 'ymoayrebzha8guruoliohglk8flxckjo',
         ],
         'cache' => [
            'class' => 'yii\caching\filecache',
         ],
         'user' => [
            'identityclass' => 'app\models\user',
            'enableautologin' => true,
         ],
         'errorhandler' => [
            'erroraction' => 'site/error',
         ],
         //other components...
            'db' => require(__dir__ . '/db.php'),
      ],
      'modules' => [
         'hello' => [
            'class' => 'app\modules\hello\hello',
         ],
      ],
      'params' => $params,
   ];
   if (yii_env_dev) {
      // configuration adjustments for 'dev' environment
      $config['bootstrap'][] = 'debug';
      $config['modules']['debug'] = [
         'class' => 'yii\debug\module',
      ];
      $config['bootstrap'][] = 'gii';
      $config['modules']['gii'] = [
         'class' => 'yii\gii\module',
      ];
   }
   return $config;
?>
the above configuration defines that when an error needs to be displayed without the call stack, the site/error action will be executed.
step 7 − modify the actions() method of the sitecontroller.
public function actions() {
   return [
      'error' => [
         'class' => 'yii\web\erroraction',
      ],
   ];
}
the above code defines, that when an error occurs, the error view will be rendered.
step 8 − create a file called error.php under the views/site directory.
<?php
   /* @var $this yii\web\view */
   /* @var $name string */
   /* @var $message string */
   /* @var $exception exception */
   use yii\helpers\html;
   $this->title = $name;
?>
<div class = "site-error">
   <h2>customized error</h2>
   <h1><?= html::encode($this->title) ?></h1>
   
   <div class = "alert alert-danger">
      <?= nl2br(html::encode($message)) ?>
   </div>
   
   <p>
      the above error occurred while the web server was processing your request.
   </p>
   
   <p>
      please contact us if you think this is a server error. thank you.
   </p>
</div>
step 9 − go to the address http://localhost:8080/index.php?r=site/show-error, you will see the customized error view.