我试图通过try catch在控制器中捕获Doctrine异常,我正在使用Symfony 3
try {
$em = $this->get('doctrine.orm.entity_manager');
$em->persist( $transaction );
$em->flush();
} catch(Exception $e) {
return new JsonResponse(['error' => 'already exist']);
}
提前致谢
最后我得到了我的问题的解决方案,我想与你分享解决方案
try {
$em = $this->get('doctrine.orm.entity_manager');
$em->persist( $transaction );
$em->flush();
} catch(\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Transaction already exist" );
} catch(\Doctrine\DBAL\Exception\ConstraintViolationException $e ) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Bad request on Transaction" );
} catch(\Doctrine\DBAL\Exception\TableNotFoundException $e ) {
throw new \Symfony\Component\HttpKernel\Exception\HttpException(409, "Transaction Table not found" );
}
此链接包含Doctrine中的所有异常
https://github.com/doctrine/dbal/tree/master/lib/Doctrine/DBAL/Exception