Completed
Push — fix-numbervalidator-comma-deci... ( cad400...eee7b9 )
by Alexander
61:03 queued 21:06
created

ErrorAction   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 0
loc 156
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 12 3
A run() 0 8 2
A renderAjaxResponse() 0 4 1
A renderHtmlResponse() 0 4 2
A getViewRenderParams() 0 8 1
A findException() 0 8 2
A getExceptionCode() 0 8 2
A getExceptionName() 0 14 3
A getExceptionMessage() 0 8 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\Action;
12
use yii\base\Exception;
13
use yii\base\UserException;
14
15
/**
16
 * ErrorAction displays application errors using a specified view.
17
 *
18
 * To use ErrorAction, you need to do the following steps:
19
 *
20
 * First, declare an action of ErrorAction type in the `actions()` method of your `SiteController`
21
 * class (or whatever controller you prefer), like the following:
22
 *
23
 * ```php
24
 * public function actions()
25
 * {
26
 *     return [
27
 *         'error' => ['class' => 'yii\web\ErrorAction'],
28
 *     ];
29
 * }
30
 * ```
31
 *
32
 * Then, create a view file for this action. If the route of your error action is `site/error`, then
33
 * the view file should be `views/site/error.php`. In this view file, the following variables are available:
34
 *
35
 * - `$name`: the error name
36
 * - `$message`: the error message
37
 * - `$exception`: the exception being handled
38
 *
39
 * Finally, configure the "errorHandler" application component as follows,
40
 *
41
 * ```php
42
 * 'errorHandler' => [
43
 *     'errorAction' => 'site/error',
44
 * ]
45
 * ```
46
 *
47
 * @author Qiang Xue <[email protected]>
48
 * @author Dmitry Naumenko <[email protected]>
49
 * @since 2.0
50
 */
51
class ErrorAction extends Action
52
{
53
    /**
54
     * @var string the view file to be rendered. If not set, it will take the value of [[id]].
55
     * That means, if you name the action as "error" in "SiteController", then the view name
56
     * would be "error", and the corresponding view file would be "views/site/error.php".
57
     */
58
    public $view;
59
    /**
60
     * @var string the name of the error when the exception name cannot be determined.
61
     * Defaults to "Error".
62
     */
63
    public $defaultName;
64
    /**
65
     * @var string the message to be displayed when the exception message contains sensitive information.
66
     * Defaults to "An internal server error occurred.".
67
     */
68
    public $defaultMessage;
69
    /**
70
     * @var \Exception the exception object, normally is filled on [[init()]] method call.
71
     * @see [[findException()]] to know default way of obtaining exception.
72
     * @since 2.0.11
73
     */
74
    protected $exception;
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function init()
80
    {
81
        $this->exception = $this->findException();
82
83
        if ($this->defaultMessage === null) {
84
            $this->defaultMessage = Yii::t('yii', 'An internal server error occurred.');
85
        }
86
87
        if ($this->defaultName === null) {
88
            $this->defaultName = Yii::t('yii', 'Error');
89
        }
90
    }
91
92
    /**
93
     * Runs the action.
94
     *
95
     * @return string result content
96
     */
97
    public function run()
98
    {
99
        if (Yii::$app->getRequest()->getIsAjax()) {
100
            return $this->renderAjaxResponse();
101
        }
102
103
        return $this->renderHtmlResponse();
104
    }
105
106
    /**
107
     * Builds string that represents the exception.
108
     * Normally used to generate a response to AJAX request.
109
     * @return string
110
     * @since 2.0.11
111
     */
112
    protected function renderAjaxResponse()
113
    {
114
        return $this->getExceptionName() . ': ' . $this->getExceptionMessage();
115
    }
116
117
    /**
118
     * Renders a view that represents the exception.
119
     * @return string
120
     * @since 2.0.11
121
     */
122
    protected function renderHtmlResponse()
123
    {
124
        return $this->controller->render($this->view ?: $this->id, $this->getViewRenderParams());
125
    }
126
127
    /**
128
     * Builds array of parameters that will be passed to the view.
129
     * @return array
130
     * @since 2.0.11
131
     */
132
    protected function getViewRenderParams()
133
    {
134
        return [
135
            'name' => $this->getExceptionName(),
136
            'message' => $this->getExceptionMessage(),
137
            'exception' => $this->exception,
138
        ];
139
    }
140
141
    /**
142
     * Gets exception from the [[yii\web\ErrorHandler|ErrorHandler]] component.
143
     * In case there is no exception in the component, treat as the action has been invoked
144
     * not from error handler, but by direct route, so '404 Not Found' error will be displayed.
145
     * @return \Exception
146
     * @since 2.0.11
147
     */
148
    protected function findException()
149
    {
150
        if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
151
            $exception = new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
152
        }
153
154
        return $exception;
155
    }
156
157
    /**
158
     * Gets the code from the [[exception]].
159
     * @return mixed
160
     * @since 2.0.11
161
     */
162
    protected function getExceptionCode()
163
    {
164
        if ($this->exception instanceof HttpException) {
165
            return $this->exception->statusCode;
166
        }
167
168
        return $this->exception->getCode();
169
    }
170
171
    /**
172
     * Returns the exception name, followed by the code (if present).
173
     *
174
     * @return string
175
     * @since 2.0.11
176
     */
177
    protected function getExceptionName()
178
    {
179
        if ($this->exception instanceof Exception) {
180
            $name = $this->exception->getName();
181
        } else {
182
            $name = $this->defaultName;
183
        }
184
185
        if ($code = $this->getExceptionCode()) {
186
            $name .= " (#$code)";
187
        }
188
189
        return $name;
190
    }
191
192
    /**
193
     * Returns the [[exception]] message for [[yii\base\UserException]] only.
194
     * For other cases [[defaultMessage]] will be returned.
195
     * @return string
196
     * @since 2.0.11
197
     */
198
    protected function getExceptionMessage()
199
    {
200
        if ($this->exception instanceof UserException) {
201
            return $this->exception->getMessage();
202
        }
203
204
        return $this->defaultMessage;
205
    }
206
}
207