Closed as not planned
Description
Symfony version(s) affected
6.2.*
Description
When my app is in debug mode I get a normal message. As soon as I switch to product mode, the message is replaced with "Bad Request".
The problem is that I did not find anywhere in the documentation how to solve this problem.
How to reproduce
<?php
namespace App\Http\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/custom')]
class CustomController extends AbstractController
{
public function __invoke()
{
throw new BadRequestHttpException('Custom error message');
}
}
If APP_ENV=dev
{
"type": "https:\/\/tools.ietf.org\/html\/rfc2616#section-10",
"title": "An` error occurred",
"status" :400,
"detail": "Custom error message",
"class": "..."
"trace": [ ]
}
If APP_ENV=prod
{
"type": "https:\/\/tools.ietf.org\/html\/rfc2616#section-10",
"title": "An` error occurred",
"status" :400,
"detail": "Bad Request",
}
Possible Solution
Temporarily solved the problem like this
#[AsEventListener(event: ExceptionEvent::class)]
public function onException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if ($exception instanceof BadRequestHttpException) {
$response = new JsonResponse([
'title' => 'An error occurred',
'status' => $exception->getStatusCode(),
'detail' => $exception->getMessage(),
], $exception->getStatusCode());
$event->setResponse($response);
}
}
Additional Context
No response