mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-09 09:47:25 +08:00
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user