.../EntityResource/EntityResourceTestBase.php | 2 +- .../EventSubscriber/DefaultExceptionSubscriber.php | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 0c10678..9f326bc 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -508,7 +508,7 @@ public function testGet() { // DX: 406 when requesting unsupported format but specifying Accept header: - // should still result in a response with the same Content-Type. + // should result in a text/plain response. $response = $this->request('GET', $url, $request_options); $this->assert406Response($response); $this->assertSame(['text/plain; charset=UTF-8'], $response->getHeader('Content-Type')); diff --git a/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php b/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php index 5a21bf4..b157f62 100644 --- a/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php +++ b/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php @@ -63,16 +63,20 @@ protected static function getPriority() { */ public function onException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); - if (in_array($event->getRequest()->getRequestFormat(), $this->getHandledFormats(), TRUE)) { - if (!$exception instanceof HttpExceptionInterface) { - $error = Error::decodeException($exception); - $message = error_displayable($error) - ? sprintf('A fatal error occurred: %s', $exception->getMessage()) - : 'A fatal error occurred.'; - $event->setException(new HttpException(500, $message, $exception)); - } + + // Map non-HttpExceptionInterface exceptions for fatal errors onto the same + // HttpExceptionInterface object, so we can handle them consistently. + if (!$exception instanceof HttpExceptionInterface && in_array($event->getRequest()->getRequestFormat(), $this->getHandledFormats(), TRUE)) { + $error = Error::decodeException($exception); + $message = error_displayable($error) + ? sprintf('A fatal error occurred: %s', $exception->getMessage()) + : 'A fatal error occurred.'; + $event->setException(new HttpException(500, $message, $exception)); } + parent::onException($event); + + // If we didn't generate an error response, restore the original exception. if (!$event->isPropagationStopped()) { $event->setException($exception); }