Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to change ExceptionException 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...