I'm using Symfony 4 with Doctrine and am handling an entity which will be persisted.
I would like to know how can I have a kind of try\catch handling when an exception occurs while persisting, so that I can send different errors accordingly.
As of now my code looks like this
$user->setUsername($username)
->setEmail($email)
->setPassword($password);
$em->persist($user);
$em->flush();
return $this->json(['msg'=>'some message', 201);
I would like to be able to decide what to send in the Response in case there are errors.