diff --git a/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php b/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php index bc1210a..5774a31 100644 --- a/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php +++ b/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php @@ -3,10 +3,8 @@ namespace Drupal\serialization\EventSubscriber; use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase; -use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; -use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\Serializer\SerializerInterface; /** @@ -58,26 +56,69 @@ protected static function getPriority() { } /** - * {@inheritdoc} + * Handles a 400 error for HTTP. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event + * The event to process. */ - public function onException(GetResponseForExceptionEvent $event) { - $exception = $event->getException(); + public function on400(GetResponseForExceptionEvent $event) { + $this->setEventResponse($event, Response::HTTP_BAD_REQUEST); + } - // Make the exception available for example when rendering a block. - $request = $event->getRequest(); - $request->attributes->set('exception', $exception); + /** + * Handles a 403 error for HTTP. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event + * The event to process. + */ + public function on403(GetResponseForExceptionEvent $event) { + $this->setEventResponse($event, Response::HTTP_FORBIDDEN); + } - $handled_formats = $this->getHandledFormats(); + /** + * Handles a 404 error for HTTP. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event + * The event to process. + */ + public function on404(GetResponseForExceptionEvent $event) { + $this->setEventResponse($event, Response::HTTP_NOT_FOUND); + } - $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat()); + /** + * Handles a 405 error for HTTP. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event + * The event to process. + */ + public function on405(GetResponseForExceptionEvent $event) { + $this->setEventResponse($event, Response::HTTP_METHOD_NOT_ALLOWED); + } - if ($exception instanceof HttpExceptionInterface && in_array($format, $handled_formats)) { - $format = $event->getRequest()->getRequestFormat(); - $content = ['message' => $event->getException()->getMessage()]; - $encoded_content = $this->serializer->serialize($content, $format); - $response = new Response($encoded_content, $exception->getStatusCode()); - $event->setResponse($response); - } + /** + * Handles a 406 error for HTTP. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event + * The event to process. + */ + public function on406(GetResponseForExceptionEvent $event) { + $this->setEventResponse($event, Response::HTTP_NOT_ACCEPTABLE); + } + + /** + * Sets the Response for the exception event. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event + * The current exception event. + * @param int $status + * The HTTP status code to set for the response. + */ + protected function setEventResponse(GetResponseForExceptionEvent $event, $status) { + $format = $event->getRequest()->getRequestFormat(); + $content = ['message' => $event->getException()->getMessage()]; + $encoded_content = $this->serializer->serialize($content, $format); + $response = new Response($encoded_content, $status); + $event->setResponse($response); } } diff --git a/core/modules/user/tests/src/Functional/UserLoginHttpTest.php b/core/modules/user/tests/src/Functional/UserLoginHttpTest.php index 7484b8c..6d4b020 100644 --- a/core/modules/user/tests/src/Functional/UserLoginHttpTest.php +++ b/core/modules/user/tests/src/Functional/UserLoginHttpTest.php @@ -106,7 +106,7 @@ public function testLogin() { $user_login_status_url->setRouteParameter('_format', $format); $user_login_status_url->setAbsolute(); - $response = $client->post($user_login_status_url->toString()); + $response = $client->get($user_login_status_url->toString()); $this->assertResponse($response, 200, UserAuthenticationController::LOGGED_OUT); // Flooded. @@ -163,14 +163,14 @@ public function testLogin() { $result_data = $this->decode($response->getBody(), $format); $this->assertEquals($name, $result_data['current_user']['name']); - $response = $client->post($user_login_status_url->toString(), ['cookies' => $this->cookies]); + $response = $client->get($user_login_status_url->toString(), ['cookies' => $this->cookies]); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(UserAuthenticationController::LOGGED_IN, (string) $response->getBody()); $response = $this->logoutRequest($format); $this->assertEquals(204, $response->getStatusCode()); - $response = $client->post($user_login_status_url->toString(), ['cookies' => $this->cookies]); + $response = $client->get($user_login_status_url->toString(), ['cookies' => $this->cookies]); $this->assertResponse($response, 200, UserAuthenticationController::LOGGED_OUT); $this->resetFlood(); diff --git a/core/modules/user/user.routing.yml b/core/modules/user/user.routing.yml index 09ac772..9cf1949 100644 --- a/core/modules/user/user.routing.yml +++ b/core/modules/user/user.routing.yml @@ -142,7 +142,7 @@ user.login_status.http: path: '/user/login_status' defaults: _controller: \Drupal\user\Controller\UserAuthenticationController::loginStatus - methods: [POST] + methods: [GET] requirements: _access: 'TRUE' _format: 'json'