Closed
Description
Symfony version(s) affected
5.4
Description
Try catch display a Symfony error and doesn't go in catch
How to reproduce
Here is the complet method
#[Route('/bordereaux/{id}', methods: ['PATCH'])]
public function patchBordereau(int $id, Request $request) : Response
{
$isUpdated = false;
$data = json_decode($request->getContent(), true);
$bordereau = $this->bordereauEntityManager->getRepository(Bordereau::class)->findOneById($id);
if(!$bordereau) {
return new Response('Bordereau non trouvé.', 204);
}
// Todo: couche de sécurité pour vérifier si le user peut patcher l'objet
foreach ($data as $property => $value) {
$propertyGetter = 'get' . ucfirst($property);
$propertySetter = 'set' . ucfirst($property);
try {
if ($bordereau->$propertyGetter() != $value) {
$bordereau->$propertySetter($value);
$isUpdated = true;
}
} catch (\Symfony\Component\ErrorHandler\Error\UndefinedMethodError $exception) {
return new Response(
sprintf("La propriété %s n'est pas une propriété de l'objet bordereau. L'objet n'a pas été mis à jour.", $property),
500
);
}
}
if ($isUpdated) {
$this->bordereauEntityManager->persist($bordereau);
$this->bordereauEntityManager->flush();
}
return new Response(
'',
$isUpdated ? 200 : 304
);
}
Possible Solution
I tried to replace UndefinedMethodError by \Exception I get the same issue.
Additional Context
No response