Skip to content

Commit 80ee2b0

Browse files
committed
Convert bad request exceptions to 400 responses
1 parent d2d140b commit 80ee2b0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of Packagist.
5+
*
6+
* (c) Jordi Boggiano <[email protected]>
7+
* Nils Adermann <[email protected]>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace App\EventListener;
14+
15+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
16+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
17+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
18+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
19+
use Twig\Error\RuntimeError;
20+
21+
/**
22+
* Converts BadRequestException to BadRequestHttpException
23+
*/
24+
class BadRequestExceptionListener
25+
{
26+
#[AsEventListener(priority: 1000)]
27+
public function onKernelException(ExceptionEvent $event): void
28+
{
29+
$exception = $event->getThrowable();
30+
31+
if ($exception instanceof RuntimeError) {
32+
$exception = $exception->getPrevious();
33+
}
34+
if ($exception instanceof BadRequestException) {
35+
$event->setThrowable(new BadRequestHttpException($exception->getMessage(), $exception));
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)