php Symfony2 控制器不会捕获异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5689415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2 Controller won't catch exception
提问by sleeves
This is the route handler for my delete action. It works well as long as the item does not have any associations.
这是我的删除操作的路由处理程序。只要项目没有任何关联,它就可以很好地工作。
public function projectDeleteAction()
{
try {
$request = $this->get('request');
$my_id = $request->query->get('id');
$em = $this->get('doctrine.orm.entity_manager');
$item = $em->find('MyBundle:Main', $my_id);
$em->remove($item);
$em->flush();
$info = $item->getName();
$result = 0;
}
catch (Exception $e) {
$info = toString($e);
$result = -1;
}
return $this->render('MyBundle:Main:response.xml.twig',
array('info' => $info, 'result' => $result ));
}
I have already solved the error of trying to delete an item with associations, but through this process, the "flush" was throwing PDOException. I tried various ways to catch it, but it appears to be getting caught inside Symfony2 and then it responds with a HTTP 500 error. Is there a way that I can have Symfony2 not catch this so that I can handle it? This is an XML response using AJAX and so I would rather just send an error code per above.
我已经解决了尝试删除具有关联的项目的错误,但是通过这个过程,“刷新”抛出了 PDOException。我尝试了各种方法来捕获它,但它似乎在 Symfony2 中被捕获,然后它以 HTTP 500 错误响应。有什么方法可以让 Symfony2 不捕捉到它,以便我可以处理它?这是一个使用 AJAX 的 XML 响应,所以我宁愿只发送上面的错误代码。
回答by Josef Cech
Try to change Exception
→ \Exception
if you didn't specified PDOException
as Exception
in a use statement. PHP tries to find \YourNamespaceWithController\Exception
instead of \Exception
(and it does not check the existence of such exception).
如果您没有在 use 语句中指定as ,请尝试更改Exception
→ 。PHP 尝试查找而不是(并且它不检查此类异常的存在)。\Exception
PDOException
Exception
\YourNamespaceWithController\Exception
\Exception
回答by Rudy Broersma
It is better to catch the exception you really want to catch. In this example that is probably Doctrine/DBAL/DBALException and/or Doctrine/DBA/DBAException.
最好捕获您真正想要捕获的异常。在这个例子中,可能是 Doctrine/DBAL/DBALException 和/或 Doctrine/DBA/DBAException。
Thus
因此
catch (Doctrine\DBAL\DBALException $e) {
$result = -1;
};
I would recomment doing something like:
我会建议做类似的事情:
} catch (\Exception $e) {
switch (get_class($e)) {
case 'Doctrine\DBAL\DBALException':
echo "DBAL Exception<br />";
break;
case 'Doctrine\DBA\DBAException':
echo "DBA Exception<br />";
break;
default:
throw $e;
break;
}
}
This actually catches the DB exceptions, and if for some reason some other exception occures, this is rethrown back into Symfony2.
这实际上捕获了 DB 异常,如果由于某种原因发生了其他一些异常,则将其重新抛出回 Symfony2。
回答by Jurgen
I had to do the following which might help for some users;
我必须执行以下操作,这可能对某些用户有所帮助;
try{
$this->doctrine->em->persist($user);
$this->doctrine->em->flush();
}catch(Exception $e){
if($e->getPrevious()->getCode() == 23505){
//handle duplicate error, 23505 is for postgres, 23000 is mysql unique constraint.
}
}