From c7bfe49a2b8c2b929e8e6fcc4571ba1b7ec671b2 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 29 May 2019 14:41:50 +0100
Subject: [PATCH] =?UTF-8?q?Issue=20#2924860=20by=20Kng,=20AdamPS:=20?=
 =?UTF-8?q?=E2=80=9CUncaught=20exception=E2=80=9D=20page=20contains=20HTML?=
 =?UTF-8?q?=20but=20has=20content-type=20text/plain?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php  | 3 ++-
 .../Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php
index 8cbf49d91a8e..186be771c9d3 100644
--- a/core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php
@@ -123,9 +123,10 @@ public function onException(GetResponseForExceptionEvent $event) {
       }
     }
 
+    $content_type = $event->getRequest()->getRequestFormat() == 'html' ? 'text/html' : 'text/plain';
     $content = $this->t('The website encountered an unexpected error. Please try again later.');
     $content .= $message ? '</br></br>' . $message : '';
-    $response = new Response($content, 500, ['Content-Type' => 'text/plain']);
+    $response = new Response($content, 500, ['Content-Type' => $content_type]);
 
     if ($exception instanceof HttpExceptionInterface) {
       $response->setStatusCode($exception->getStatusCode());
diff --git a/core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php b/core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php
index 3215d9ce5689..2dae1147b8ab 100644
--- a/core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php
@@ -157,7 +157,7 @@ public function testBacktraceEscaping() {
     $kernel = \Drupal::getContainer()->get('http_kernel');
     $response = $kernel->handle($request)->prepare($request);
     $this->assertEqual($response->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
-    $this->assertEqual($response->headers->get('Content-type'), 'text/plain; charset=UTF-8');
+    $this->assertEqual($response->headers->get('Content-type'), 'text/html; charset=UTF-8');
 
     // Test both that the backtrace is properly escaped, and that the unescaped
     // string is not output at all.
@@ -180,7 +180,7 @@ public function testExceptionEscaping() {
     $kernel = \Drupal::getContainer()->get('http_kernel');
     $response = $kernel->handle($request)->prepare($request);
     $this->assertEqual($response->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
-    $this->assertEqual($response->headers->get('Content-type'), 'text/plain; charset=UTF-8');
+    $this->assertEqual($response->headers->get('Content-type'), 'text/html; charset=UTF-8');
 
     // Test message is properly escaped, and that the unescaped string is not
     // output at all.
-- 
GitLab