Skip to content

Commit 9d8778c

Browse files
built-in events subscriber
1 parent bddc4da commit 9d8778c

File tree

5 files changed

+165
-2
lines changed

5 files changed

+165
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"symfony/messenger": "6.0.*",
2020
"symfony/proxy-manager-bridge": "6.0.*",
2121
"symfony/runtime": "6.0.*",
22+
"symfony/serializer": "6.0.*",
2223
"symfony/uid": "6.0.*",
2324
"symfony/yaml": "6.0.*"
2425
},

composer.lock

Lines changed: 102 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ services:
3737
App\Shared\Infrastructure\Doctrine\DomainEventSubscriber:
3838
tags: [ { name: 'doctrine.event_subscriber' } ]
3939
arguments:
40-
$containerBag: '@service_container'
40+
$containerBag: '@service_container'
41+
42+
43+
App\Core\EventListener\BuiltInEventsSubscriber:
44+
tags:
45+
- { name: kernel.event_subscriber }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Core\EventListener;
6+
7+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
use Symfony\Component\HttpFoundation\JsonResponse;
9+
use Symfony\Component\HttpFoundation\Response;
10+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
11+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
12+
use Symfony\Component\HttpKernel\KernelEvents;
13+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
14+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
15+
use Symfony\Component\Serializer\Serializer;
16+
17+
class BuiltInEventsSubscriber implements EventSubscriberInterface
18+
{
19+
public function onKernelException(ExceptionEvent $event)
20+
{
21+
$acceptHeader = $event->getRequest()->headers->get('Accept');
22+
23+
if ('application/json' == $acceptHeader) {
24+
$exception = $event->getThrowable();
25+
26+
$response = new JsonResponse();
27+
28+
$encoders = [new JsonEncoder()];
29+
$normalizers = [new ObjectNormalizer()];
30+
$serializer = new Serializer($normalizers, $encoders);
31+
32+
$response->setContent($serializer->serialize($exception, 'json'));
33+
34+
if ($exception instanceof HttpExceptionInterface) {
35+
$response->setStatusCode($exception->getStatusCode());
36+
$response->headers->replace($exception->getHeaders());
37+
} else {
38+
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
39+
}
40+
$event->setResponse($response);
41+
}
42+
}
43+
44+
public static function getSubscribedEvents()
45+
{
46+
return [
47+
KernelEvents::EXCEPTION => 'onKernelException',
48+
];
49+
}
50+
}

symfony.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
"gesdinet/jwt-refresh-token-bundle": {
129129
"version": "v1.0.1"
130130
},
131+
"jetbrains/phpstorm-attributes": {
132+
"version": "1.0"
133+
},
131134
"laminas/laminas-code": {
132135
"version": "4.5.1"
133136
},
@@ -477,6 +480,9 @@
477480
"symfony/security-http": {
478481
"version": "v6.0.5"
479482
},
483+
"symfony/serializer": {
484+
"version": "v6.0.12"
485+
},
480486
"symfony/service-contracts": {
481487
"version": "v3.0.0"
482488
},

0 commit comments

Comments
 (0)