commit 2e2bbf6c35d0bf3dde6b64abc97e9d22e8007c51 Author: Erik Stielstra Date: Wed Oct 5 09:09:45 2016 +0200 Add check for matching route. 2810293-4. diff --git a/src/EventSubscriber/ExceptionSubscriber.php b/src/EventSubscriber/ExceptionSubscriber.php index 06cf502..98a2929 100644 --- a/src/EventSubscriber/ExceptionSubscriber.php +++ b/src/EventSubscriber/ExceptionSubscriber.php @@ -41,7 +41,7 @@ class ExceptionSubscriber implements EventSubscriberInterface { * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack */ public function __construct(RequestStack $request_stack, SerializerInterface $serializer) { - $this->currentRequest = $request_stack; + $this->currentRequest = $request_stack->getCurrentRequest(); $this->serializer = $serializer; } @@ -52,12 +52,13 @@ class ExceptionSubscriber implements EventSubscriberInterface { * The event to process. */ public function onException(GetResponseForExceptionEvent $event) { - // @todo Check if applies. - // Check if route is the JSON API route. - //$this->currentRequest; - $exception = $event->getException(); + // Only act if route is the JSON API route. + if (!$this->applies()) { + return; + } + $exception = $event->getException(); if (!$exception instanceof HttpExceptionInterface) { // If this is a generic exception, generate a Server Exception HTTP // exception and process it. @@ -86,4 +87,16 @@ class ExceptionSubscriber implements EventSubscriberInterface { return $events; } + /** + * Check if the the current route is a JSON API route. + * + * @return bool + * Returns true of the route applies. + */ + protected function applies() { + // @todo Is this cheching for '_format' reliable? + // @todo Is this (kind of) check used in other places too? + return $this->currentRequest->query->get('_format') == 'api_json'; + } + }