Browse Source

Refactored `yii\web\ErrorAction` to make it reusable

tags/2.0.11
SilverFire - Dmitry Naumenko 8 years ago
parent
commit
24ad05de5b
No known key found for this signature in database
GPG Key ID: 39DD917A92B270A
  1. 1
      framework/CHANGELOG.md
  2. 142
      framework/web/ErrorAction.php
  3. 14
      tests/data/views/error.php
  4. 100
      tests/framework/web/ErrorActionTest.php

1
framework/CHANGELOG.md

@ -89,6 +89,7 @@ Yii Framework 2 Change Log
- Enh #13122: Optimized query for information about foreign keys in `yii\db\oci` (zlakomanoff) - Enh #13122: Optimized query for information about foreign keys in `yii\db\oci` (zlakomanoff)
- Enh #13202: Refactor validateAttribute method in UniqueValidator (developeruz) - Enh #13202: Refactor validateAttribute method in UniqueValidator (developeruz)
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe) - Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
- Enh: Refactored `yii\web\ErrorAction` to make it reusable (silverfire)
2.0.10 October 20, 2016 2.0.10 October 20, 2016

142
framework/web/ErrorAction.php

@ -45,6 +45,7 @@ use yii\base\UserException;
* ``` * ```
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @author Dmitry Naumenko <d.naumenko.a@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class ErrorAction extends Action class ErrorAction extends Action
@ -65,48 +66,141 @@ class ErrorAction extends Action
* Defaults to "An internal server error occurred.". * Defaults to "An internal server error occurred.".
*/ */
public $defaultMessage; public $defaultMessage;
/**
* @var \Exception the exception object, normally is filled on [[init()]] method call.
* @see [[findException()]] to know default way of obtaining exception.
* @since 2.0.11
*/
protected $exception;
/**
* {@inheritdoc}
*/
public function init()
{
$this->exception = $this->findException();
if ($this->defaultMessage === null) {
$this->defaultMessage = Yii::t('yii', 'An internal server error occurred.');
}
if ($this->defaultName === null) {
$this->defaultName = Yii::t('yii', 'Error');
}
}
/** /**
* Runs the action * Runs the action.
* *
* @return string result content * @return string result content
*/ */
public function run() public function run()
{ {
if (Yii::$app->getRequest()->getIsAjax()) {
return $this->renderAjaxResponse();
}
return $this->renderHtmlResponse();
}
/**
* Builds string that represents the exception.
* Normally used to generate a response to AJAX request.
* @return string
* @since 2.0.11
*/
protected function renderAjaxResponse()
{
return $this->getExceptionName() . ': ' . $this->getExceptionMessage();
}
/**
* Renders a view that represents the exception.
* @return string
* @since 2.0.11
*/
protected function renderHtmlResponse()
{
return $this->controller->render($this->view ?: $this->id, $this->getViewRenderParams());
}
/**
* Builds array of parameters that will be passed to the view.
* @return array
* @since 2.0.11
*/
protected function getViewRenderParams()
{
return [
'name' => $this->getExceptionName(),
'message' => $this->getExceptionMessage(),
'exception' => $this->exception,
];
}
/**
* Gets exception from the [[yii\web\ErrorHandler|ErrorHandler]] component.
* In case there is no exception in the component, treat as the action has been invoked
* not from error handler, but by direct route, so '404 Not Found' error will be displayed.
* @return \Exception
* @since 2.0.11
*/
protected function findException()
{
if (($exception = Yii::$app->getErrorHandler()->exception) === null) { if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
// action has been invoked not from error handler, but by direct route, so we display '404 Not Found' $exception = new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
$exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
} }
if ($exception instanceof HttpException) { return $exception;
$code = $exception->statusCode; }
} else {
$code = $exception->getCode(); /**
* Gets the code from the [[exception]].
* @return mixed
* @since 2.0.11
*/
protected function getExceptionCode()
{
if ($this->exception instanceof HttpException) {
return $this->exception->statusCode;
} }
if ($exception instanceof Exception) {
$name = $exception->getName(); return $this->exception->getCode();
}
/**
* Returns the exception name, followed by the code (if present).
*
* @return string
* @since 2.0.11
*/
protected function getExceptionName()
{
if ($this->exception instanceof Exception) {
$name = $this->exception->getName();
} else { } else {
$name = $this->defaultName ?: Yii::t('yii', 'Error'); $name = $this->defaultName;
} }
if ($code) {
if ($code = $this->getExceptionCode()) {
$name .= " (#$code)"; $name .= " (#$code)";
} }
if ($exception instanceof UserException) { return $name;
$message = $exception->getMessage(); }
} else {
$message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
}
if (Yii::$app->getRequest()->getIsAjax()) { /**
return "$name: $message"; * Returns the [[exception]] message for [[yii\base\UserException]] only.
} else { * For other cases [[defaultMessage]] will be returned.
return $this->controller->render($this->view ?: $this->id, [ * @return string
'name' => $name, * @since 2.0.11
'message' => $message, */
'exception' => $exception, protected function getExceptionMessage()
]); {
if ($this->exception instanceof UserException) {
return $this->exception->getMessage();
} }
return $this->defaultMessage;
} }
} }

14
tests/data/views/error.php

@ -0,0 +1,14 @@
<?php
/**
* @var string $name
* @var string $message
* @var Exception $exception
*/
?>
Name: <?= $name ?>
Message: <?= $message ?>
Exception: <?= get_class($exception) ?>

100
tests/framework/web/ErrorActionTest.php

@ -0,0 +1,100 @@
<?php
namespace yiiunit\framework\web;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\UserException;
use yii\web\Controller;
use yii\web\ErrorAction;
use yii\web\Request;
use yiiunit\TestCase;
/**
* @group web
*/
class ErrorActionTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication([
'components' => [
'request' => [
'class' => Request::className(),
],
],
]);
}
public function getController($actionConfig = [])
{
return new TestController('test', Yii::$app, ['layout' => false, 'actionConfig' => $actionConfig]);
}
public function testGeneralException()
{
Yii::$app->getErrorHandler()->exception = new InvalidConfigException('This message will not be shown to user');
$this->assertEquals('Name: Invalid Configuration
Message: An internal server error occurred.
Exception: yii\base\InvalidConfigException', $this->getController()->runAction('error'));
}
public function testUserException()
{
Yii::$app->getErrorHandler()->exception = new UserException('User can see this error message');
$this->assertEquals('Name: Exception
Message: User can see this error message
Exception: yii\base\UserException', $this->getController()->runAction('error'));
}
public function testAjaxRequest()
{
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->assertEquals('Not Found (#404): Page not found.', $this->getController()->runAction('error'));
}
public function testNoExceptionInHandler()
{
$this->assertEquals('Name: Not Found (#404)
Message: Page not found.
Exception: yii\web\NotFoundHttpException', $this->getController()->runAction('error'));
}
public function testDefaultView()
{
/** @var ErrorAction $action */
$action = $this->getController()->createAction('error');
// Unset view name. Class should try to load view that matches action name by default
$action->view = null;
$this->setExpectedExceptionRegExp('yii\base\ViewNotFoundException', '#The view file does not exist: .*?views/test/error.php#');
$this->invokeMethod($action, 'renderHtmlResponse');
}
}
class TestController extends Controller
{
private $actionConfig;
public function setActionConfig($config = [])
{
$this->actionConfig = $config;
}
public function actions()
{
return [
'error' => array_merge([
'class' => ErrorAction::className(),
'view' => '@yiiunit/data/views/error.php',
], $this->actionConfig),
];
}
}
Loading…
Cancel
Save