I'm struggling with passing message from constraint used in QueryParam, for instance:
class MyController extends FOSRestController
{
/**
* @param ParamFetcherInterface $paramFetcher
* @return Response
*
* @Rest\Get("/reports/my_raport")
* @Rest\QueryParam(name="filter", strict=true, requirements=@App\Validator\API\Constraints\Filter)
*/
public function referralFoundsAccountUsage(ParamFetcherInterface $paramFetcher): Response
{
return new Response();
}
}
My fos_rest.yaml:
fos_rest:
body_listener: true
param_fetcher_listener: true
exception:
enabled: true
exception_controller: 'App\Controller\API\ExceptionController::showAction'
format_listener:
enabled: true
rules:
- { path: ^/api, prefer_extension: false, priorities: ['json'], fallback_format: json }
I want to pass error message returned by constraint to ExceptionController, so I can format it as json.
I found the answer:
class ExceptionController extends FOSRestController
{
public function showAction(Request $request, \Throwable $exception, DebugLoggerInterface $logger = null): Response
{
return new JsonApiResponse($exception->getMessage(), 400);
}
}