Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fixes #14711: Fixed yii\web\ErrorHandler displaying exception messa…
…ge in non-debug mode
  • Loading branch information
samdark committed Jan 22, 2018
1 parent a2a79a7 commit 6b0be47
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.14 under development
------------------------

- Bug #14711: Fixed `yii\web\ErrorHandler` displaying exception message in non-debug mode (samdark)
- Enh #13814: MySQL unique index names can now contain spaces (df2)
- Bug #15300: Fixed "Cannot read property 'style' of undefined" error at the error screen (vitorarantes)
- Enh #15426: Added abilitiy to create and drop database views (igravity, vladis84)
Expand Down
42 changes: 28 additions & 14 deletions framework/base/ErrorHandler.php
Expand Up @@ -324,22 +324,36 @@ public static function convertExceptionToError($exception)
*/
public static function convertExceptionToString($exception)
{
if ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) {
$message = "{$exception->getName()}: {$exception->getMessage()}";
} elseif (YII_DEBUG) {
if ($exception instanceof Exception) {
$message = "Exception ({$exception->getName()})";
} elseif ($exception instanceof ErrorException) {
$message = "{$exception->getName()}";
} else {
$message = 'Exception';
}
$message .= " '" . get_class($exception) . "' with message '{$exception->getMessage()}' \n\nin "
. $exception->getFile() . ':' . $exception->getLine() . "\n\n"
. "Stack trace:\n" . $exception->getTraceAsString();
if ($exception instanceof UserException) {
return "{$exception->getName()}: {$exception->getMessage()}";
}

if (YII_DEBUG) {
return static::convertExceptionToVerboseString($exception);
}

return 'An internal server error occurred.';
}

/**
* Converts an exception into a string that has verbose information about the exception and its trace.
* @param \Exception|\Error $exception the exception being converted
* @return string the string representation of the exception.
*
* @since 2.0.14
*/
public static function convertExceptionToVerboseString($exception)
{
if ($exception instanceof Exception) {
$message = "Exception ({$exception->getName()})";
} elseif ($exception instanceof ErrorException) {
$message = "{$exception->getName()}";
} else {
$message = 'Error: ' . $exception->getMessage();
$message = 'Exception';
}
$message .= " '" . get_class($exception) . "' with message '{$exception->getMessage()}' \n\nin "
. $exception->getFile() . ':' . $exception->getLine() . "\n\n"
. "Stack trace:\n" . $exception->getTraceAsString();

return $message;
}
Expand Down
2 changes: 1 addition & 1 deletion framework/log/Dispatcher.php
Expand Up @@ -190,7 +190,7 @@ public function dispatch($messages, $final)
} catch (\Exception $e) {
$target->enabled = false;
$targetErrors[] = [
'Unable to send log via ' . get_class($target) . ': ' . ErrorHandler::convertExceptionToString($e),
'Unable to send log via ' . get_class($target) . ': ' . ErrorHandler::convertExceptionToVerboseString($e),
Logger::LEVEL_WARNING,
__METHOD__,
microtime(true),
Expand Down
1 change: 0 additions & 1 deletion framework/views/errorHandler/exception.php
Expand Up @@ -17,7 +17,6 @@
if ($exception instanceof \yii\web\HttpException) {
echo (int) $exception->statusCode . ' ' . $handler->htmlEncode($name);
} else {
$name = $handler->getExceptionName($exception);
if ($name !== null) {
echo $handler->htmlEncode($name . ' – ' . get_class($exception));
} else {
Expand Down
34 changes: 27 additions & 7 deletions tests/framework/log/DispatcherTest.php
Expand Up @@ -203,13 +203,33 @@ public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect()
->withConsecutive(
[$this->equalTo('messages'), $this->equalTo(true)],
[
[[
'Unable to send log via ' . get_class($target1) . ': Exception: some error',
Logger::LEVEL_WARNING,
'yii\log\Dispatcher::dispatch',
'time data',
[],
]],
$this->callback(function($arg) use ($target1) {
if (!isset($arg[0][0], $arg[0][1], $arg[0][2], $arg[0][3])) {
return false;
}

if (strpos($arg[0][0], 'Unable to send log via ' . get_class($target1) . ': Exception (Exception) \'yii\base\UserException\' with message \'some error\'') !== 0) {
return false;
}

if ($arg[0][1] !== Logger::LEVEL_WARNING) {
return false;
}

if ($arg[0][2] !== 'yii\log\Dispatcher::dispatch') {
return false;
}

if ($arg[0][3] !== 'time data') {
return false;
}

if ($arg[0][4] !== []) {
return false;
}

return true;
}),
true,
]
);
Expand Down

1 comment on commit 6b0be47

@SilverFire
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.