2016-09-22 41 views
2

我正在與Symfony 2一起工作,並且我正在嘗試製作一個小問題求解器包。從控制器中執行的命令拋出異常

我的實體Problem包含一個retry字段,其​​中包含有關要執行哪個命令以重試以前失敗的信息的信息。

因此,這裏是我的控制器代碼:

public function retryAction($id) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $problem = $em->getRepository('RBLogsBundle:Problem')->find($id); 

    $kernel = $this->get('kernel'); 

    $application = new Application($kernel); 
    $application->setAutoExit(false); 

    $input = new ArrayInput($problem->getRetry()); 

    $output = new BufferedOutput(); 
    $application->run($input,$output); 

    $content = $output->fetch(); 

    return new Response($content); 
} 

當我的命令失敗,它通常會拋出異常。然而,在這裏,命令期間拋出的Exception似乎停止了命令,但我的控制器的執行仍在繼續。

我怎麼能在我的控制器中知道命令結束了好還是壞?

+0

目前,我唯一的解決方案是在'輸出'查找'[Exception]'字符串,但它的感覺是如此,這麼糟糕的做法... – Hammerbot

回答

1

對不起,我在Application類中找到了包含setCatchExceptions()方法的解決方案。解決方案如下:

$application = new Application($kernel); 
$application->setCatchExceptions(false); 
+0

你有沒有看到[ConsoleEvents :: EXCEPTION](https://symfony.com/doc/current/components/console/events.html#the-consoleevents-exception-event) – doydoy44

+0

不,我沒有看到,非常感謝您的分享! – Hammerbot

+0

不客氣。你應該驗證你的答案:) – doydoy44