Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

ExceptionListener.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\EventListener;
8
9 use eZ\Publish\API\Repository\Exceptions\BadStateException;
10 use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
11 use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12 use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
13 use eZ\Publish\Core\Base\Translatable;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
17 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
18 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
19 use Symfony\Component\HttpKernel\Exception\HttpException;
20 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21 use Symfony\Component\HttpKernel\KernelEvents;
22 use Symfony\Component\Translation\TranslatorInterface;
23 use Exception;
24
25 class ExceptionListener implements EventSubscriberInterface
26 {
28 private $translator;
29
30 public function __construct(TranslatorInterface $translator)
31 {
32 $this->translator = $translator;
33 }
34
35 public static function getSubscribedEvents()
36 {
37 return [
38 KernelEvents::EXCEPTION => ['onKernelException', 10],
39 ];
40 }
41
42 public function onKernelException(GetResponseForExceptionEvent $event)
43 {
44 $exception = $event->getException();
45
46 if ($exception instanceof NotFoundException) {
47 $event->setException(new NotFoundHttpException($this->getTranslatedMessage($exception), $exception));
48 } elseif ($exception instanceof UnauthorizedException) {
49 $event->setException(new AccessDeniedException($this->getTranslatedMessage($exception), $exception));
50 } elseif ($exception instanceof BadStateException || $exception instanceof InvalidArgumentException) {
51 $event->setException(new BadRequestHttpException($this->getTranslatedMessage($exception), $exception));
52 } elseif ($exception instanceof Translatable) {
53 $event->setException(
54 new HttpException(
55 Response::HTTP_INTERNAL_SERVER_ERROR,
56 get_class($exception) . ': ' . $this->getTranslatedMessage($exception),
57 $exception
58 )
59 );
60 }
61 }
62
70 private function getTranslatedMessage(Exception $exception)
71 {
72 $message = $exception->getMessage();
73 if ($exception instanceof Translatable) {
74 $message = $this->translator->trans($exception->getMessageTemplate(), $exception->getParameters(), 'repository_exceptions');
75 }
76
77 return $message;
78 }
79 }
eZ\Bundle\EzPublishCoreBundle\EventListener\ExceptionListener
Definition: ExceptionListener.php:26
eZ\Bundle\EzPublishCoreBundle\EventListener\ExceptionListener\getTranslatedMessage
getTranslatedMessage(Exception $exception)
Translates the exception message if it is translatable.
Definition: ExceptionListener.php:70
eZ\Bundle\EzPublishCoreBundle\EventListener\ExceptionListener\getSubscribedEvents
static getSubscribedEvents()
Definition: ExceptionListener.php:35
eZ\Bundle\EzPublishCoreBundle\EventListener\ExceptionListener\__construct
__construct(TranslatorInterface $translator)
Definition: ExceptionListener.php:30
eZ\Bundle\EzPublishCoreBundle\EventListener\ExceptionListener\$translator
$translator
Definition: ExceptionListener.php:28
eZ\Bundle\EzPublishCoreBundle\EventListener\ExceptionListener\onKernelException
onKernelException(GetResponseForExceptionEvent $event)
Definition: ExceptionListener.php:42
eZ\Publish\API\Repository\Exceptions\BadStateException
This Exception is thrown if a method is called with an value referencing an object which is not in th...
Definition: API/Repository/Exceptions/BadStateException.php:13
eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
This exception is thrown if a service method is called with an illegal or non appropriate value.
Definition: API/Repository/Exceptions/InvalidArgumentException.php:13
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Exceptions\UnauthorizedException
This Exception is thrown if the user has is not allowed to perform a service operation.
Definition: API/Repository/Exceptions/UnauthorizedException.php:15
eZ\Publish\Core\Base\Translatable
Interface for translatable value objects.
Definition: Core/Base/Translatable.php:13
eZ\Bundle\EzPublishCoreBundle\EventListener
Definition: BackgroundIndexingTerminateListener.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.