ErrorAction.php 5.5 KB

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