From 7dd1b2f3f702f242e9a5c45b17f24f04857cebc3 Mon Sep 17 00:00:00 2001
From: catch <catch56@gmail.com>
Date: Tue, 8 Nov 2022 09:29:38 +0000
Subject: [PATCH] Issue #3319170 by longwave, effulgentsia: Change the
 http_kernel.basic service to use Symfony 6.2's default of catching all
 throwables

(cherry picked from commit 37c60ccb610b8b0644a67cd95439a17faf423626)
---
 core/core.services.yml                        |  2 +-
 .../src/MonkeysInTheControlRoom.php           | 15 +-----------
 .../Functional/System/ErrorHandlerTest.php    | 24 +++++++++++++++++++
 .../Bootstrap/UncaughtExceptionTest.php       | 21 ----------------
 4 files changed, 26 insertions(+), 36 deletions(-)

diff --git a/core/core.services.yml b/core/core.services.yml
index 9e06de33f394..8e5c79d7b83a 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -743,7 +743,7 @@ services:
     class: Drupal\Core\StackMiddleware\StackedHttpKernel
   http_kernel.basic:
     class: Symfony\Component\HttpKernel\HttpKernel
-    arguments: ['@event_dispatcher', '@controller_resolver', '@request_stack', '@http_kernel.controller.argument_resolver', false]
+    arguments: ['@event_dispatcher', '@controller_resolver', '@request_stack', '@http_kernel.controller.argument_resolver', true]
   http_kernel.controller.argument_resolver:
     class: Symfony\Component\HttpKernel\Controller\ArgumentResolver
     arguments: ['@http_kernel.controller.argument_metadata_factory', ['@argument_resolver.request_attribute', '@argument_resolver.request', '@argument_resolver.psr7_request', '@argument_resolver.route_match', '@argument_resolver.default']]
diff --git a/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php b/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php
index 2d08efc6fc9e..cef5bb64b81a 100644
--- a/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php
+++ b/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php
@@ -67,20 +67,7 @@ public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TR
       throw new \Exception('Deforestation');
     }
 
-    if ($this->settings->get('teapots', FALSE) && class_exists('\TypeError')) {
-      try {
-        $return = $this->app->handle($request, $type, $catch);
-      }
-      catch (\TypeError $e) {
-        header('HTTP/1.1 418 I\'m a teapot');
-        print('Oh oh, flying teapots');
-        exit;
-      }
-    }
-    else {
-      $return = $this->app->handle($request, $type, $catch);
-    }
-    return $return;
+    return $this->app->handle($request, $type, $catch);
   }
 
 }
diff --git a/core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php b/core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php
index 26daf6ec1c9d..e9007b51d51f 100644
--- a/core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php
+++ b/core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php
@@ -92,6 +92,30 @@ public function testErrorHandler() {
     $this->assertSession()->responseNotContains('<pre class="backtrace">');
   }
 
+  /**
+   * Tests a custom error handler set in settings.php.
+   */
+  public function testCustomErrorHandler() {
+    $settings_filename = $this->siteDirectory . '/settings.php';
+    chmod($settings_filename, 0777);
+    $settings_php = file_get_contents($settings_filename);
+    $settings_php .= "\n";
+    $settings_php .= "set_error_handler(function() {\n";
+    $settings_php .= "  header('HTTP/1.1 418 I\'m a teapot');\n";
+    $settings_php .= "  print('Oh oh, flying teapots from a custom error handler');\n";
+    $settings_php .= "  exit();\n";
+    $settings_php .= "});\n";
+    file_put_contents($settings_filename, $settings_php);
+
+    // For most types of errors, PHP throws an \Error object that Drupal
+    // catches, so the error handler is not invoked. To test the error handler,
+    // generate warnings, which are not thrown/caught.
+    $this->drupalGet('error-test/generate-warnings');
+
+    $this->assertSession()->statusCodeEquals(418);
+    $this->assertSession()->responseContains('Oh oh, flying teapots from a custom error handler');
+  }
+
   /**
    * Tests the exception handler.
    */
diff --git a/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php b/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php
index b03e000b48cf..fbf557fb1b28 100644
--- a/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php
+++ b/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php
@@ -143,27 +143,6 @@ public function testMissingDependency() {
     $this->assertErrorLogged($this->expectedExceptionMessage);
   }
 
-  /**
-   * Tests a missing dependency on a service with a custom error handler.
-   */
-  public function testMissingDependencyCustomErrorHandler() {
-    $settings_filename = $this->siteDirectory . '/settings.php';
-    chmod($settings_filename, 0777);
-    $settings_php = file_get_contents($settings_filename);
-    $settings_php .= "\n";
-    $settings_php .= "set_error_handler(function() {\n";
-    $settings_php .= "  header('HTTP/1.1 418 I\'m a teapot');\n";
-    $settings_php .= "  print('Oh oh, flying teapots');\n";
-    $settings_php .= "  exit();\n";
-    $settings_php .= "});\n";
-    $settings_php .= "\$settings['teapots'] = TRUE;\n";
-    file_put_contents($settings_filename, $settings_php);
-
-    $this->drupalGet('broken-service-class');
-    $this->assertSession()->statusCodeEquals(418);
-    $this->assertSession()->responseContains('Oh oh, flying teapots');
-  }
-
   /**
    * Tests a container which has an error.
    */
-- 
GitLab