From 0415f597980c2aff8e8953c24ea5b5019e9bd25c Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sun, 20 Feb 2022 09:43:10 +0000
Subject: [PATCH] Issue #3260044 by kim.pepper, andypost, daffie, mfb: Add a
 constant for watchdog_exception message

---
 core/includes/bootstrap.inc                            |  2 +-
 core/includes/errors.inc                               | 10 +++++-----
 core/includes/update.inc                               |  4 ++--
 .../EventSubscriber/DefaultExceptionHtmlSubscriber.php |  2 +-
 .../EventSubscriber/ExceptionLoggingSubscriber.php     |  4 ++--
 .../Core/EventSubscriber/FinalExceptionSubscriber.php  |  4 ++--
 core/lib/Drupal/Core/Utility/Error.php                 |  7 ++++++-
 7 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index 6ac76e28cbd9..29d96d1ab01c 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -129,7 +129,7 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia
 
   // Use a default value if $message is not set.
   if (empty($message)) {
-    $message = '%type: @message in %function (line %line of %file).';
+    $message = Error::DEFAULT_ERROR_MESSAGE;
   }
 
   if ($link) {
diff --git a/core/includes/errors.inc b/core/includes/errors.inc
index 79328b3e5c58..a4bffb2f3b11 100644
--- a/core/includes/errors.inc
+++ b/core/includes/errors.inc
@@ -177,7 +177,7 @@ function _drupal_log_error($error, $fatal = FALSE) {
     catch (\Exception $e) {
       // We can't log, for example because the database connection is not
       // available. At least try to log to PHP error log.
-      error_log(strtr('Failed to log error: %type: @message in %function (line %line of %file). @backtrace_string', $error));
+      error_log(strtr('Failed to log error: ' . Error::DEFAULT_ERROR_MESSAGE . ' @backtrace_string', $error));
     }
   }
 
@@ -190,7 +190,7 @@ function _drupal_log_error($error, $fatal = FALSE) {
     if ($fatal) {
       // When called from CLI, simply output a plain text message.
       // Should not translate the string to avoid errors producing more errors.
-      $response->setContent(html_entity_decode(strip_tags(new FormattableMarkup('%type: @message in %function (line %line of %file).', $error))) . "\n");
+      $response->setContent(html_entity_decode(strip_tags(new FormattableMarkup(Error::DEFAULT_ERROR_MESSAGE, $error))) . "\n");
       $response->send();
       exit(1);
     }
@@ -201,7 +201,7 @@ function _drupal_log_error($error, $fatal = FALSE) {
       if (error_displayable($error)) {
         // When called from JavaScript, simply output the error message.
         // Should not translate the string to avoid errors producing more errors.
-        $response->setContent(new FormattableMarkup('%type: @message in %function (line %line of %file).', $error));
+        $response->setContent(new FormattableMarkup(Error::DEFAULT_ERROR_MESSAGE, $error));
         $response->send();
       }
       exit;
@@ -240,7 +240,7 @@ function _drupal_log_error($error, $fatal = FALSE) {
         // We use \Drupal\Component\Render\FormattableMarkup directly here,
         // rather than use t() since we are in the middle of error handling, and
         // we don't want t() to cause further errors.
-        $message = new FormattableMarkup('%type: @message in %function (line %line of %file).', $error);
+        $message = new FormattableMarkup(Error::DEFAULT_ERROR_MESSAGE, $error);
       }
       else {
         // With verbose logging, we will also include a backtrace.
@@ -252,7 +252,7 @@ function _drupal_log_error($error, $fatal = FALSE) {
         array_shift($backtrace);
         // Generate a backtrace containing only scalar argument values.
         $error['@backtrace'] = Error::formatBacktrace($backtrace);
-        $message = new FormattableMarkup('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $error);
+        $message = new FormattableMarkup(Error::DEFAULT_ERROR_MESSAGE . ' <pre class="backtrace">@backtrace</pre>', $error);
       }
     }
 
diff --git a/core/includes/update.inc b/core/includes/update.inc
index a1bab071246d..22caf2924270 100644
--- a/core/includes/update.inc
+++ b/core/includes/update.inc
@@ -179,7 +179,7 @@ function update_do_one($module, $number, $dependency_map, &$context) {
 
       $variables = Error::decodeException($e);
       unset($variables['backtrace'], $variables['exception'], $variables['severity_level']);
-      $ret['#abort'] = ['success' => FALSE, 'query' => t('%type: @message in %function (line %line of %file).', $variables)];
+      $ret['#abort'] = ['success' => FALSE, 'query' => t(Error::DEFAULT_ERROR_MESSAGE, $variables)];
     }
   }
 
@@ -248,7 +248,7 @@ function update_invoke_post_update($function, &$context) {
       unset($variables['backtrace'], $variables['exception']);
       $ret['#abort'] = [
         'success' => FALSE,
-        'query' => t('%type: @message in %function (line %line of %file).', $variables),
+        'query' => t(Error::DEFAULT_ERROR_MESSAGE, $variables),
       ];
     }
   }
diff --git a/core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php
index c7920d3c4cdb..615f2ee3c01d 100644
--- a/core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php
@@ -190,7 +190,7 @@ protected function makeSubrequest(ExceptionEvent $event, $url, $status_code) {
       // just log it. The DefaultExceptionSubscriber will catch the original
       // exception and handle it normally.
       $error = Error::decodeException($e);
-      $this->logger->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
+      $this->logger->log($error['severity_level'], Error::DEFAULT_ERROR_MESSAGE, $error);
     }
   }
 
diff --git a/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
index 83e22bf9d283..526cb42d69ae 100644
--- a/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
@@ -44,7 +44,7 @@ public function on403(ExceptionEvent $event) {
     $error = Error::decodeException($exception);
     unset($error['@backtrace_string']);
     $error['@uri'] = $event->getRequest()->getRequestUri();
-    $this->logger->get('access denied')->warning('Path: @uri. %type: @message in %function (line %line of %file).', $error);
+    $this->logger->get('access denied')->warning('Path: @uri. ' . Error::DEFAULT_ERROR_MESSAGE, $error);
   }
 
   /**
@@ -67,7 +67,7 @@ public function on404(ExceptionEvent $event) {
   public function onError(ExceptionEvent $event) {
     $exception = $event->getThrowable();
     $error = Error::decodeException($exception);
-    $this->logger->get('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
+    $this->logger->get('php')->log($error['severity_level'], Error::DEFAULT_ERROR_MESSAGE, $error);
 
     $is_critical = !$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500;
     if ($is_critical) {
diff --git a/core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php
index c940fea3ba5b..3933875768f4 100644
--- a/core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php
@@ -100,7 +100,7 @@ public function onException(ExceptionEvent $event) {
         // We use \Drupal\Component\Render\FormattableMarkup directly here,
         // rather than use t() since we are in the middle of error handling, and
         // we don't want t() to cause further errors.
-        $message = new FormattableMarkup('%type: @message in %function (line %line of %file).', $error);
+        $message = new FormattableMarkup(Error::DEFAULT_ERROR_MESSAGE, $error);
       }
       else {
         // With verbose logging, we will also include a backtrace.
@@ -118,7 +118,7 @@ public function onException(ExceptionEvent $event) {
 
         // Generate a backtrace containing only scalar argument values.
         $error['@backtrace'] = Error::formatBacktrace($backtrace);
-        $message = new FormattableMarkup('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $error);
+        $message = new FormattableMarkup(Error::DEFAULT_ERROR_MESSAGE . ' <pre class="backtrace">@backtrace</pre>', $error);
       }
     }
 
diff --git a/core/lib/Drupal/Core/Utility/Error.php b/core/lib/Drupal/Core/Utility/Error.php
index 8cec41f8ba49..31ed9dd8e858 100644
--- a/core/lib/Drupal/Core/Utility/Error.php
+++ b/core/lib/Drupal/Core/Utility/Error.php
@@ -20,6 +20,11 @@ class Error {
    */
   const ERROR = 3;
 
+  /**
+   * The the default message for logging errors.
+   */
+  const DEFAULT_ERROR_MESSAGE = '%type: @message in %function (line %line of %file).';
+
   /**
    * An array of ignored functions.
    *
@@ -92,7 +97,7 @@ public static function renderExceptionSafe($exception) {
     // no longer function correctly (as opposed to a user-triggered error), so
     // we assume that it is safe to include a verbose backtrace.
     $decode['@backtrace'] = Error::formatBacktrace($backtrace);
-    return new FormattableMarkup('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $decode);
+    return new FormattableMarkup(Error::DEFAULT_ERROR_MESSAGE . ' <pre class="backtrace">@backtrace</pre>', $decode);
   }
 
   /**
-- 
GitLab