Commit 6e7fc11e authored by catch's avatar catch

Issue #2949419 by mfb, vsujeetkumar, gapple, Hardik_Patel_12, neclimdul,...

Issue #2949419 by mfb, vsujeetkumar, gapple, Hardik_Patel_12, neclimdul, dawehner, Sam152, xjm, alexpott: Pass the raw exception to logger implementations
parent 783e7eb8
...@@ -79,6 +79,7 @@ function _drupal_error_handler_real($error_level, $message, $filename, $line) { ...@@ -79,6 +79,7 @@ function _drupal_error_handler_real($error_level, $message, $filename, $line) {
'severity_level' => $severity_level, 'severity_level' => $severity_level,
'backtrace' => $backtrace, 'backtrace' => $backtrace,
'@backtrace_string' => (new \Exception())->getTraceAsString(), '@backtrace_string' => (new \Exception())->getTraceAsString(),
'exception' => NULL,
], $recoverable || $to_string); ], $recoverable || $to_string);
} }
// If the site is a test site then fail for user deprecations so they can be // If the site is a test site then fail for user deprecations so they can be
...@@ -133,10 +134,12 @@ function error_displayable($error = NULL) { ...@@ -133,10 +134,12 @@ function error_displayable($error = NULL) {
* Logs a PHP error or exception and displays an error page in fatal cases. * Logs a PHP error or exception and displays an error page in fatal cases.
* *
* @param $error * @param $error
* An array with the following keys: %type, @message, %function, %file, * An array with the following keys: %type, @message, %function, %file, %line,
* %line, @backtrace_string, severity_level, and backtrace. All the parameters * @backtrace_string, severity_level, backtrace, and exception. All the
* are plain-text, with the exception of @message, which needs to be an HTML * parameters are plain-text, with the exception of @message, which needs to
* string, and backtrace, which is a standard PHP backtrace. * be an HTML string, backtrace, which is a standard PHP backtrace, and
* exception, which is the exception object (or NULL if the error is not an
* exception).
* @param bool $fatal * @param bool $fatal
* TRUE for: * TRUE for:
* - An exception is thrown and not caught by something else. * - An exception is thrown and not caught by something else.
...@@ -146,13 +149,12 @@ function error_displayable($error = NULL) { ...@@ -146,13 +149,12 @@ function error_displayable($error = NULL) {
function _drupal_log_error($error, $fatal = FALSE) { function _drupal_log_error($error, $fatal = FALSE) {
$is_installer = InstallerKernel::installationAttempted(); $is_installer = InstallerKernel::installationAttempted();
// Remove 'severity_level' as is not a valid replacement for t(). // Backtrace, exception and 'severity_level' are not valid replacement values
$severity = $error['severity_level']; // for t().
unset($error['severity_level']);
// Backtrace array is not a valid replacement value for t().
$backtrace = $error['backtrace']; $backtrace = $error['backtrace'];
unset($error['backtrace']); $exception = $error['exception'];
$severity = $error['severity_level'];
unset($error['backtrace'], $error['exception'], $error['severity_level']);
// When running inside the testing framework, we relay the errors // When running inside the testing framework, we relay the errors
// to the tested site by the way of HTTP headers. // to the tested site by the way of HTTP headers.
...@@ -167,10 +169,10 @@ function _drupal_log_error($error, $fatal = FALSE) { ...@@ -167,10 +169,10 @@ function _drupal_log_error($error, $fatal = FALSE) {
// installer. // installer.
if (\Drupal::hasService('logger.factory')) { if (\Drupal::hasService('logger.factory')) {
try { try {
// Provide the PHP backtrace to logger implementations. Add // Provide the PHP backtrace and exception to logger implementations. Add
// 'severity_level' to the context to maintain BC and allow logging // 'severity_level' to the context to maintain BC and allow logging
// implementations to use it. // implementations to use it.
\Drupal::logger('php')->log($severity, '%type: @message in %function (line %line of %file) @backtrace_string.', $error + ['backtrace' => $backtrace, 'severity_level' => $severity]); \Drupal::logger('php')->log($severity, '%type: @message in %function (line %line of %file) @backtrace_string.', $error + ['backtrace' => $backtrace, 'exception' => $exception, 'severity_level' => $severity]);
} }
catch (\Exception $e) { catch (\Exception $e) {
// We can't log, for example because the database connection is not // We can't log, for example because the database connection is not
......
...@@ -231,8 +231,7 @@ function update_do_one($module, $number, $dependency_map, &$context) { ...@@ -231,8 +231,7 @@ function update_do_one($module, $number, $dependency_map, &$context) {
watchdog_exception('update', $e); watchdog_exception('update', $e);
$variables = Error::decodeException($e); $variables = Error::decodeException($e);
unset($variables['backtrace']); unset($variables['backtrace'], $variables['exception'], $variables['severity_level']);
unset($variables['severity_level']);
$ret['#abort'] = ['success' => FALSE, 'query' => t('%type: @message in %function (line %line of %file).', $variables)]; $ret['#abort'] = ['success' => FALSE, 'query' => t('%type: @message in %function (line %line of %file).', $variables)];
} }
} }
...@@ -299,7 +298,7 @@ function update_invoke_post_update($function, &$context) { ...@@ -299,7 +298,7 @@ function update_invoke_post_update($function, &$context) {
watchdog_exception('update', $e); watchdog_exception('update', $e);
$variables = Error::decodeException($e); $variables = Error::decodeException($e);
unset($variables['backtrace']); unset($variables['backtrace'], $variables['exception']);
$ret['#abort'] = [ $ret['#abort'] = [
'success' => FALSE, 'success' => FALSE,
'query' => t('%type: @message in %function (line %line of %file).', $variables), 'query' => t('%type: @message in %function (line %line of %file).', $variables),
......
...@@ -93,8 +93,7 @@ public function onException(ExceptionEvent $event) { ...@@ -93,8 +93,7 @@ public function onException(ExceptionEvent $event) {
$error = $this->simplifyFileInError($error); $error = $this->simplifyFileInError($error);
unset($error['backtrace']); unset($error['backtrace'], $error['exception'], $error['severity_level']);
unset($error['severity_level']);
if (!$this->isErrorLevelVerbose()) { if (!$this->isErrorLevelVerbose()) {
// Without verbose logging, use a simple message. // Without verbose logging, use a simple message.
......
...@@ -71,6 +71,7 @@ public static function decodeException($exception) { ...@@ -71,6 +71,7 @@ public static function decodeException($exception) {
'severity_level' => static::ERROR, 'severity_level' => static::ERROR,
'backtrace' => $backtrace, 'backtrace' => $backtrace,
'@backtrace_string' => $exception->getTraceAsString(), '@backtrace_string' => $exception->getTraceAsString(),
'exception' => $exception,
]; ];
} }
...@@ -86,7 +87,7 @@ public static function decodeException($exception) { ...@@ -86,7 +87,7 @@ public static function decodeException($exception) {
public static function renderExceptionSafe($exception) { public static function renderExceptionSafe($exception) {
$decode = static::decodeException($exception); $decode = static::decodeException($exception);
$backtrace = $decode['backtrace']; $backtrace = $decode['backtrace'];
unset($decode['backtrace']); unset($decode['backtrace'], $decode['exception']);
// Remove 'main()'. // Remove 'main()'.
array_shift($backtrace); array_shift($backtrace);
......
...@@ -53,8 +53,8 @@ public function __construct(Connection $connection, LogMessageParserInterface $p ...@@ -53,8 +53,8 @@ public function __construct(Connection $connection, LogMessageParserInterface $p
* {@inheritdoc} * {@inheritdoc}
*/ */
public function log($level, $message, array $context = []) { public function log($level, $message, array $context = []) {
// Remove any backtraces since they may contain an unserializable variable. // Remove backtrace and exception since they may contain an unserializable variable.
unset($context['backtrace']); unset($context['backtrace'], $context['exception']);
// Convert PSR3-style messages to \Drupal\Component\Render\FormattableMarkup // Convert PSR3-style messages to \Drupal\Component\Render\FormattableMarkup
// style, so they can be translated too in runtime. // style, so they can be translated too in runtime.
......
...@@ -35,6 +35,7 @@ public function testFatalExitCode() { ...@@ -35,6 +35,7 @@ public function testFatalExitCode() {
'@backtrace_string' => 'backtrace', '@backtrace_string' => 'backtrace',
'severity_level' => 0, 'severity_level' => 0,
'backtrace' => [], 'backtrace' => [],
'exception' => NULL,
]; ];
_drupal_log_error($error, TRUE); _drupal_log_error($error, TRUE);
EOT; EOT;
......
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