error.md 2.83 KB
Newer Older
Alexander Makarov committed
1 2 3
Error Handling
==============

Alexander Makarov committed
4 5 6 7
Error handling in Yii is different than handling errors in plain PHP. First of all, Yii will convert all non-fatal errors
to *exceptions*. By doing so, you can gracefully handle them using `try`-`catch`. Second, even fatal errors in Yii are
rendered in a nice way. This means that in debugging mode, you can trace the causes of fatal errors in order to more quickly
identify the cause of the problem.
8

Larry Ullman committed
9 10
Rendering errors in a dedicated controller action
-------------------------------------------------
11

Alexander Makarov committed
12 13 14
The default Yii error page is great when developing a site, and is acceptable for production sites if `YII_DEBUG`
is turned off in your bootstrap index.php file. But but you may want to customize the default error page to make it
more suitable for your project.
Larry Ullman committed
15

Alexander Makarov committed
16 17
The easiest way to create a custom error page it is to use a dedicated controller action for error rendering. First,
you'll need to configure the `errorHandler` component in the application's configuration:
18 19 20 21 22 23 24 25 26 27 28

```php
return [
    // ...
    'components' => [
        // ...
        'errorHandler' => [
            'errorAction' => 'site/error',
    ],
```

Alexander Makarov committed
29 30
With that configuration in place, whenever an error occurs, Yii will execute the "error" action of the "Site" controller.
That action should look for an exception and, if present, render the proper view file, passing along the exception:
31 32 33 34

```php
public function actionError()
{
35 36 37
    if (\Yii::$app->exception !== null) {
        return $this->render('error', ['exception' => \Yii::$app->exception]);
    }
38 39 40
}
```

Alexander Makarov committed
41 42
Next, you would create the `views/site/error.php` file, which would make use of the exception. The exception object has
the following properties:
Larry Ullman committed
43

Alexander Makarov committed
44 45 46 47 48 49 50 51
- `statusCode`: the HTTP status code (e.g. 403, 500). Available for HTTP exceptions only.
- `code`: the code of the exception.
- `type`: the error type (e.g. HttpException, PHP Error).
- `message`: the error message.
- `file`: the name of the PHP script file where the error occurs.
- `line`: the line number of the code where the error occurs.
- `trace`: the call stack of the error.
- `source`: the context source code where the error occurs.
Larry Ullman committed
52 53 54 55

Rendering errors without a dedicated controller action
------------------------------------------------------

Alexander Makarov committed
56 57
Instead of creating a dedicated action within the Site controller, you could just indicate to Yii what class should
be used to handle errors:
58 59 60 61 62 63 64 65 66 67 68 69

```php
public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
    ];
}
```

Alexander Makarov committed
70 71
After associating the class with the error as in the above, define the `views/site/error.php` file, which will
automatically be used. The view will be passed three variables:
72 73 74 75 76

- `$name`: the error name
- `$message`: the error message
- `$exception`: the exception being handled

Vladimir committed
77
The `$exception` object will have the same properties outlined above.