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

Alexander Makarov committed
4
Error handling in Yii is different than handling errors in plain PHP. First of all, Yii will convert all non-fatal errors
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
to *exceptions*:

```php
use yii\base\ErrorException;
use Yii;

try {
	10/0;
} catch (ErrorException) {
	Yii::warning("Tried dividing by zero.");
}

// execution may continue
```

As demonstrated above you may handle errors 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.
25

Larry Ullman committed
26 27
Rendering errors in a dedicated controller action
-------------------------------------------------
28

Alexander Makarov committed
29 30 31
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
32

Alexander Makarov committed
33 34
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:
35 36 37 38 39 40 41 42 43 44 45

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

Alexander Makarov committed
46 47
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:
48 49 50 51

```php
public function actionError()
{
52 53 54
    if (\Yii::$app->exception !== null) {
        return $this->render('error', ['exception' => \Yii::$app->exception]);
    }
55 56 57
}
```

Alexander Makarov committed
58 59
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
60

Alexander Makarov committed
61 62 63 64 65 66 67 68
- `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
69 70 71 72

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

Alexander Makarov committed
73 74
Instead of creating a dedicated action within the Site controller, you could just indicate to Yii what class should
be used to handle errors:
75 76 77 78 79 80 81 82 83 84 85 86

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

Alexander Makarov committed
87 88
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:
89 90 91 92 93

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

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