@@ -38,4 +38,109 @@ to the "require" section of your composer.json.
38
38
Usage
39
39
-----
40
40
41
- This extension provides Enhanced Error Handler for Yii1 application.
41
+ This extension provides Enhanced Error Handler for Yii1 application.
42
+ Its main feature is conversion of the PHP errors into exceptions, so they may be processed via ` try..catch ` blocks.
43
+
44
+ > Note: in order for error to exception conversion to work, the error handler component should be added to the
45
+ application "preload" section.
46
+
47
+ Application configuration example:
48
+
49
+ ``` php
50
+ <?php
51
+
52
+ return [
53
+ 'preload' => [
54
+ 'errorHandler', // preload custom error handler, overriding the default one, allowing error to exception conversion
55
+ // ...
56
+ ],
57
+ 'components' => [
58
+ 'errorHandler' => [
59
+ 'class' => \yii1tech\error\handler\ErrorHandler::class,
60
+ ],
61
+ // ...
62
+ ],
63
+ // ...
64
+ ];
65
+ ```
66
+
67
+ Once configured ` \yii1tech\error\handler\ErrorHandler ` allows you to catch PHP errors as exceptions.
68
+ For example:
69
+
70
+ ``` php
71
+ <?php
72
+
73
+ try {
74
+ // ...
75
+ trigger_error('Some custom error message', E_USER_WARNING);
76
+ // ...
77
+ } catch (ErrorException $exception) {
78
+ // handle error
79
+ }
80
+ ```
81
+
82
+ In addition, ` \yii1tech\error\handler\ErrorHandler ` provides support for error/exception rendering as JSON output,
83
+ which is useful for modern XHR and API implementation.
84
+ By default, the error will be rendered as JSON only in case the HTTP client passes header "Accept" with matching MIME type -
85
+ "application/json". However, you may control this behavior using ` \yii1tech\error\handler\ErrorHandler::$shouldRenderErrorAsJsonCallback ` .
86
+ For example:
87
+
88
+ ``` php
89
+ <?php
90
+
91
+ return [
92
+ 'preload' => [
93
+ 'errorHandler', // preload custom error handler, overriding the default one, allowing error to exception conversion
94
+ // ...
95
+ ],
96
+ 'components' => [
97
+ 'errorHandler' => [
98
+ 'class' => \yii1tech\error\handler\ErrorHandler::class,
99
+ 'shouldRenderErrorAsJsonCallback' => function () {
100
+ if (!empty($_SERVER['HTTP_ACCEPT']) && strcasecmp($_SERVER['HTTP_ACCEPT'], 'application/json') === 0) {
101
+ return true;
102
+ }
103
+
104
+ if (Yii::app()->request->isAjaxRequest) {
105
+ return true;
106
+ }
107
+
108
+ if (str_starts_with(Yii::app()->request->getPathInfo(), 'api/')) {
109
+ return true;
110
+ }
111
+
112
+ return false;
113
+ },
114
+ ],
115
+ // ...
116
+ ],
117
+ // ...
118
+ ];
119
+ ```
120
+
121
+ You may reuse the error handler ability to render errors as JSON in your custom error action, which is specified
122
+ via ` \CErrorHandler::$errorAction ` . For example:
123
+
124
+ ``` php
125
+ <?php
126
+
127
+ class SiteController extends CController
128
+ {
129
+ public function actionError(): void
130
+ {
131
+ /** @var \yii1tech\error\handler\ErrorHandler $errorHandler */
132
+ $errorHandler = Yii::app()->getErrorHandler();
133
+ if ($errorHandler->shouldRenderErrorAsJson()) {
134
+ // render JSON error representation.
135
+ $errorHandler->renderErrorAsJson();
136
+
137
+ return;
138
+ }
139
+
140
+ // Render HTML error representation:
141
+ if ($error = $errorHandler->error) {
142
+ $this->render('error', $error);
143
+ }
144
+ }
145
+ }
146
+ ```
0 commit comments