src/EventSubscriber/InternalErrorSubscriber.php line 39
<?phpnamespace App\EventSubscriber;use Psr\Log\LoggerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpKernel\Event\ExceptionEvent;use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;use Symfony\Component\HttpKernel\KernelEvents;/*** Answers every failure under /internal/ as a short JSON body, never as Symfony's error page.** With APP_DEBUG on, the error page shows the code, the query and the settings around a* failure. The account API never passes a page on to a browser, but it should not receive one* either. A failure of this service is logged here in full and answered as 500 with one* sentence; an address that names no route, or a method a route does not take, keeps its own* status (404, 405).*/final class InternalErrorSubscriber implements EventSubscriberInterface{private const SENTENCES = [400 => 'Bad request',404 => 'Not found',405 => 'Method not allowed',];public function __construct(private LoggerInterface $logger){}public static function getSubscribedEvents(): array{// Before Symfony's own error listener, which listens at -128 and renders the page.return [KernelEvents::EXCEPTION => ['onKernelException', 10]];}public function onKernelException(ExceptionEvent $event): void{if (!ServiceKeySubscriber::isInternal($event->getRequest()->getPathInfo())) {return;}$error = $event->getThrowable();if ($error instanceof HttpExceptionInterface && $error->getStatusCode() < 500) {$status = $error->getStatusCode();$event->setResponse(new JsonResponse(['error' => self::SENTENCES[$status] ?? 'Request refused'],$status,$error->getHeaders()));return;}$this->logger->error('[internal] ' . $error->getMessage(), ['exception' => $error]);$event->setResponse(new JsonResponse(['error' => 'The social service failed'], 500));}}