Symfony2 控制器不会捕获异常 [英] Symfony2 Controller won't catch exception
问题描述
这是我的删除操作的路由处理程序.只要项目没有任何关联,它就可以正常工作.
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 ));
}
我已经解决了尝试删除具有关联的项目的错误,但是通过这个过程,刷新"抛出了 PDOException.我尝试了各种方法来捕获它,但它似乎在 Symfony2 中被捕获,然后它以 HTTP 500 错误响应.有什么方法可以让 Symfony2 不捕捉到它,以便我可以处理它?这是一个使用 AJAX 的 XML 响应,所以我宁愿只发送上面的错误代码.
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.
推荐答案
Try to change Exception
→ Exception
如果你没有指定 PDOException
作为 Exception
在 use 语句中.PHP 尝试查找 YourNamespaceWithControllerException
而不是 Exception
(并且它不检查此类异常的存在).
Try to change Exception
→ Exception
if you didn't specified PDOException
as Exception
in a use statement. PHP tries to find YourNamespaceWithControllerException
instead of Exception
(and it does not check the existence of such exception).
这篇关于Symfony2 控制器不会捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!