Commit c93231e2 by Carsten Brandt

improved session error handling

fixes #1946
parent 8b4d4a0b
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace yii\mongodb; namespace yii\mongodb;
use Yii; use Yii;
use yii\base\ErrorHandler;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance; use yii\di\Instance;
...@@ -49,6 +50,7 @@ class Session extends \yii\web\Session ...@@ -49,6 +50,7 @@ class Session extends \yii\web\Session
*/ */
public $sessionCollection = 'session'; public $sessionCollection = 'session';
/** /**
* Initializes the Session component. * Initializes the Session component.
* This method will initialize the [[db]] property to make sure it refers to a valid MongoDB connection. * 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 ...@@ -148,10 +150,11 @@ class Session extends \yii\web\Session
['upsert' => true] ['upsert' => true]
); );
} catch (\Exception $e) { } catch (\Exception $e) {
if (YII_DEBUG) { $exception = ErrorHandler::convertExceptionToString($e);
echo $e->getMessage(); // its too late to use Yii logging here
} error_log($exception);
// it is too late to log an error message here echo $exception;
return false; return false;
} }
......
...@@ -246,4 +246,42 @@ class ErrorHandler extends Component ...@@ -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 @@ ...@@ -7,6 +7,7 @@
namespace yii\mail; namespace yii\mail;
use yii\base\ErrorHandler;
use yii\base\Object; use yii\base\Object;
use Yii; use Yii;
...@@ -58,7 +59,7 @@ abstract class BaseMessage extends Object implements MessageInterface ...@@ -58,7 +59,7 @@ abstract class BaseMessage extends Object implements MessageInterface
try { try {
return $this->toString(); return $this->toString();
} catch (\Exception $e) { } catch (\Exception $e) {
trigger_error($e->getMessage() . "\n\n" . $e->getTraceAsString()); ErrorHandler::convertExceptionToError($e);
return ''; return '';
} }
} }
......
...@@ -182,10 +182,11 @@ class DbSession extends Session ...@@ -182,10 +182,11 @@ class DbSession extends Session
->execute(); ->execute();
} }
} catch (\Exception $e) { } catch (\Exception $e) {
if (YII_DEBUG) { $exception = ErrorHandler::convertExceptionToString($e);
echo $e->getMessage(); // its too late to use Yii logging here
} error_log($exception);
// it is too late to log an error message here echo $exception;
return false; return false;
} }
......
...@@ -132,32 +132,6 @@ class ErrorHandler extends \yii\base\ErrorHandler ...@@ -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. * Converts special characters to HTML entities.
* @param string $text to encode. * @param string $text to encode.
* @return string encoded original text. * @return string encoded original text.
......
...@@ -8,6 +8,7 @@ namespace yii\widgets; ...@@ -8,6 +8,7 @@ namespace yii\widgets;
use Yii; use Yii;
use yii\base\Component; use yii\base\Component;
use yii\base\ErrorHandler;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html; use yii\helpers\Html;
use yii\base\Model; use yii\base\Model;
...@@ -140,8 +141,7 @@ class ActiveField extends Component ...@@ -140,8 +141,7 @@ class ActiveField extends Component
try { try {
return $this->render(); return $this->render();
} catch (\Exception $e) { } catch (\Exception $e) {
trigger_error($e->getMessage() . "\n\n" . $e->getTraceAsString()); ErrorHandler::convertExceptionToError($e);
return ''; 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