Commit c93231e2 by Carsten Brandt

improved session error handling

fixes #1946
parent 8b4d4a0b
......@@ -8,6 +8,7 @@
namespace yii\mongodb;
use Yii;
use yii\base\ErrorHandler;
use yii\base\InvalidConfigException;
use yii\di\Instance;
......@@ -49,6 +50,7 @@ class Session extends \yii\web\Session
*/
public $sessionCollection = 'session';
/**
* Initializes the Session component.
* This method will initialize the [[db]] property to make sure it refers to a valid MongoDB connection.
......@@ -148,10 +150,11 @@ class Session extends \yii\web\Session
['upsert' => true]
);
} catch (\Exception $e) {
if (YII_DEBUG) {
echo $e->getMessage();
}
// it is too late to log an error message here
$exception = ErrorHandler::convertExceptionToString($e);
// its too late to use Yii logging here
error_log($exception);
echo $exception;
return false;
}
......
......@@ -246,4 +246,42 @@ class ErrorHandler extends Component
}
}
}
/**
* Converts an exception into a PHP error.
*
* This method can be used to convert exceptions inside of methods like `__toString()`
* to PHP errors because exceptions cannot be thrown inside of them.
* @param \Exception $exception the exception to convert to a PHP error.
*/
public static function convertExceptionToError($exception)
{
trigger_error(static::convertExceptionToString($exception), E_USER_ERROR);
}
/**
* Converts an exception into a simple string.
* @param \Exception $exception the exception being converted
* @return string the string representation of the 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();
} else {
$message = 'Error: ' . $exception->getMessage();
}
return $message;
}
}
......@@ -7,6 +7,7 @@
namespace yii\mail;
use yii\base\ErrorHandler;
use yii\base\Object;
use Yii;
......@@ -58,7 +59,7 @@ abstract class BaseMessage extends Object implements MessageInterface
try {
return $this->toString();
} catch (\Exception $e) {
trigger_error($e->getMessage() . "\n\n" . $e->getTraceAsString());
ErrorHandler::convertExceptionToError($e);
return '';
}
}
......
......@@ -182,10 +182,11 @@ class DbSession extends Session
->execute();
}
} catch (\Exception $e) {
if (YII_DEBUG) {
echo $e->getMessage();
}
// it is too late to log an error message here
$exception = ErrorHandler::convertExceptionToString($e);
// its too late to use Yii logging here
error_log($exception);
echo $exception;
return false;
}
......
......@@ -132,32 +132,6 @@ class ErrorHandler extends \yii\base\ErrorHandler
}
/**
* Converts an exception into a simple string.
* @param \Exception $exception the exception being converted
* @return string the string representation of the exception.
*/
protected 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();
} else {
$message = 'Error: ' . $exception->getMessage();
}
return $message;
}
/**
* Converts special characters to HTML entities.
* @param string $text to encode.
* @return string encoded original text.
......
......@@ -8,6 +8,7 @@ namespace yii\widgets;
use Yii;
use yii\base\Component;
use yii\base\ErrorHandler;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\base\Model;
......@@ -140,8 +141,7 @@ class ActiveField extends Component
try {
return $this->render();
} catch (\Exception $e) {
trigger_error($e->getMessage() . "\n\n" . $e->getTraceAsString());
ErrorHandler::convertExceptionToError($e);
return '';
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment