error.md 2.92 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
to *exceptions*:

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

try {
12
    10/0;
13
} catch (ErrorException $e) {
14
    Yii::warning("Tried dividing by zero.");
15 16 17 18 19 20 21 22 23
}

// 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.
24

25

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

Alexander Makarov committed
29
The default Yii error page is great when developing a site, and is acceptable for production sites if `YII_DEBUG`
30
is turned off in your bootstrap `index.php` file. But you may want to customize the default error page to make it
Alexander Makarov committed
31
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

```php
37 38
// ...
'components' => [
39
    // ...
40 41
    'errorHandler' => [
        'errorAction' => 'site/error',
42
    ],
43
]
44 45
```

46
With that configuration in place, whenever an error occurs, Yii will execute the `error`-action of the `site`-controller.
Alexander Makarov committed
47
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
    $exception = \Yii::$app->errorHandler->exception;
    if ($exception !== null) {
        return $this->render('error', ['exception' => $exception]);
55
    }
56 57 58
}
```

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

62
- `statusCode`: the HTTP status code (e.g. 403, 500). Available for [[yii\web\HttpException|HTTP exceptions]] only.
Alexander Makarov committed
63 64 65 66 67
- `code`: the code of the exception.
- `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.
68

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

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