本文介绍了Symfony2控制器不会捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的删除操作的路由处理程序。只要项目没有任何关联,它就可以正常工作。

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 ));
}

我已经解决了尝试删除具有关联的项目的错误,但是通过这个过程,flush就是抛出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.

推荐答案

尝试更改异常 \Exception 如果您没有指定 PDOException as 异常在使用语句中。 PHP尝试找到 \YourNamespaceWithController\Exception 而不是 \Exception

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.

这篇关于Symfony2控制器不会捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:58