ErrorAction.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <[email protected]>
  45. * @since 2.0
  46. */
  47. class ErrorAction extends Action
  48. {
  49. /**
  50. * @var string the view file to be rendered. If not set, it will take the value of [[id]].
  51. * That means, if you name the action as "error" in "SiteController", then the view name
  52. * would be "error", and the corresponding view file would be "views/site/error.php".
  53. */
  54. public $view;
  55. /**
  56. * @var string the name of the error when the exception name cannot be determined.
  57. * Defaults to "Error".
  58. */
  59. public $defaultName;
  60. /**
  61. * @var string the message to be displayed when the exception message contains sensitive information.
  62. * Defaults to "An internal server error occurred.".
  63. */
  64. public $defaultMessage;
  65. public function run()
  66. {
  67. if (($exception = Yii::$app->exception) === null) {
  68. return '';
  69. }
  70. if ($exception instanceof HttpException) {
  71. $code = $exception->statusCode;
  72. } else {
  73. $code = $exception->getCode();
  74. }
  75. if ($exception instanceof Exception) {
  76. $name = $exception->getName();
  77. } else {
  78. $name = $this->defaultName ?: Yii::t('yii', 'Error');
  79. }
  80. if ($code) {
  81. $name .= " (#$code)";
  82. }
  83. if ($exception instanceof UserException) {
  84. $message = $exception->getMessage();
  85. } else {
  86. $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
  87. }
  88. if (Yii::$app->getRequest()->getIsAjax()) {
  89. return "$name: $message";
  90. } else {
  91. return $this->controller->render($this->view ?: $this->id, [
  92. 'name' => $name,
  93. 'message' => $message,
  94. 'exception' => $exception,
  95. ]);
  96. }
  97. }
  98. }