Skip to content

Commit 2ea2007

Browse files
committed
ADD: ExceptionListener
1 parent 2b8cb1a commit 2ea2007

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

EventListener/ExceptionListener.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
4+
namespace HalloVerden\HttpExceptionsBundle\EventListener;
5+
6+
use HalloVerden\HttpExceptionsBundle\Interfaces\Services\ExceptionConverterServiceInterface;
7+
use HalloVerden\HttpExceptionsBundle\Interfaces\Services\ExceptionLogServiceInterface;
8+
use HalloVerden\HttpExceptionsBundle\Interfaces\Services\ExceptionResponseServiceInterface;
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
11+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
12+
use Symfony\Component\HttpKernel\KernelEvents;
13+
14+
class ExceptionListener implements EventSubscriberInterface {
15+
16+
/**
17+
* @var ExceptionConverterServiceInterface
18+
*/
19+
private $exceptionConverterService;
20+
21+
/**
22+
* @var ExceptionLogServiceInterface
23+
*/
24+
private $logExceptionService;
25+
26+
/**
27+
* @var ExceptionResponseServiceInterface
28+
*/
29+
private $exceptionResponseService;
30+
31+
/**
32+
* ExceptionListener constructor.
33+
*
34+
* @param ExceptionConverterServiceInterface $exceptionConverterService
35+
* @param ExceptionLogServiceInterface $logExceptionService
36+
* @param ExceptionResponseServiceInterface $exceptionResponseService
37+
*/
38+
public function __construct(ExceptionConverterServiceInterface $exceptionConverterService,
39+
ExceptionLogServiceInterface $logExceptionService,
40+
ExceptionResponseServiceInterface $exceptionResponseService) {
41+
$this->exceptionConverterService = $exceptionConverterService;
42+
$this->logExceptionService = $logExceptionService;
43+
$this->exceptionResponseService = $exceptionResponseService;
44+
}
45+
46+
/**
47+
* @param ExceptionEvent $event
48+
*/
49+
public function convertKernelException(ExceptionEvent $event) {
50+
$throwable = $event->getThrowable();
51+
52+
if (!$throwable instanceof HttpExceptionInterface) {
53+
$event->setThrowable($this->exceptionConverterService->createHttpExceptionFromThrowable($event->getThrowable()));
54+
}
55+
}
56+
57+
/**
58+
* @param ExceptionEvent $event
59+
*/
60+
public function logKernelException(ExceptionEvent $event) {
61+
$exception = $event->getThrowable();
62+
if (!$exception instanceof HttpExceptionInterface) {
63+
return;
64+
}
65+
66+
$this->logExceptionService->logHttpException($exception);
67+
}
68+
69+
70+
/**
71+
* @param ExceptionEvent $event
72+
*/
73+
public function onKernelException(ExceptionEvent $event) {
74+
$exception = $event->getThrowable();
75+
if (!$exception instanceof HttpExceptionInterface) {
76+
return;
77+
}
78+
79+
$event->setResponse($this->exceptionResponseService->createResponseFromHttpException($exception));
80+
}
81+
82+
/**
83+
* @inheritDoc
84+
*/
85+
public static function getSubscribedEvents() {
86+
return array(
87+
KernelEvents::EXCEPTION => [
88+
['convertKernelException', 2048],
89+
['logKernelException', 1024],
90+
['onKernelException', 32]
91+
],
92+
);
93+
}
94+
}

0 commit comments

Comments
 (0)