从PHP/Symfony中的异常恢复
我使用了Symfony2,它调用了一个服务,在出现错误时抛出一个从PHP/Symfony中的异常恢复,php,symfony,exception-handling,daemon,Php,Symfony,Exception Handling,Daemon,我使用了Symfony2,它调用了一个服务,在出现错误时抛出一个UniverseException。有点像这样 # /src/AppBundle/Command/UniverseCommand.php class UniverseCommand extends ContainerAwareCommand { protected function execute(InputInterface $input, OutputInterface $output) { if
UniverseException
。有点像这样
# /src/AppBundle/Command/UniverseCommand.php
class UniverseCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
if ( 1 == 2 )
throw new UniverseException('Strange things are afoot');
}
}
我还添加了一个CommandExceptionListener
,如果抛出MyAppBundleException
,它可以恢复我的应用程序
# /src/AppBundle/EventListener/CommandExceptionListener.php
class CommandExceptionListener
{
public function onConsoleException(ConsoleExceptionEvent $event)
{
if ($exception instanceof UniverseException) {
// Reboot the universe
// Continue existence..?
}
}
}
它工作得很好
但是现在我想使用包作为守护进程运行该命令。如果我的服务抛出异常,执行将停止,这将导致一个糟糕的守护进程
我的应用程序已经处理了此类异常。有没有办法从中恢复并允许我的守护进程继续执行
编辑:
我尝试在命令中添加一个try
/catch
,如下所示
# /src/AppBundle/Command/UniverseCommand.php
class UniverseCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
try{
if ( 1 == 2 )
throw new UniverseException('Strange things are afoot');
}catch(\Exception $e){
echo 'The universe behaved badly but I rebooted it.';
}
}
}
它捕获异常,守护进程继续!当然,现在我的事件侦听器没有启动,异常也没有得到正确处理。可能该命令以某种方式返回非零退出状态,导致执行停止?@nateevans我尝试设置
$this->setReturnCode(0)代码>在我的execute()函数中,但没有运气,这就是你的意思吗?