2017-07-28 39 views
3

我有一个非常简单的函数来检查的实体是否存在捆绑:捕获不同的异常类型

public function checkExists($bundle, $val) 
{ 
    try{ 
     $this->em->getRepository($bundle.':'.$val); 
    }catch (MappingException $e){ 
     return false; 
    } 

    return true; 
} 

所以我有以下情况:

Input      | Expected | Actual 
'AppBundle', 'Company'  | true  | true 
'AppBundle', 'NONEXISTANT' | false  | false (MappingException caught) 
'NONEXISTANT', 'Company'  | false  | 500 (ORMException not caught) 
'NONEXISTANT', 'NONEXISTANT' | false  | 500 (ORMException not caught) 

所以我看到这个问题是有不同的异常抛出,但我怎么能返回假的任何一个部分不存在的情况?有没有一种“一般”的方式来捕捉symfony中的异常,因为catch (Exception $e)use Symfony\Component\Config\Definition\Exception\Exception;没有捕捉到它。

+0

可能重复[在一个catch块中捕获多个异常类型](https://stackoverflow.com/questions/8439581/catching-multiple-exception-types-in-one-catch-block) – jkucharovic

+1

你真的不'不想依赖于这种东西的例外。看看Doctrine的元数据。特别是:$ em-> getClassMetadata($ entityClassName); – Cerad

回答

3

有几件事情要做: 您可以捕获所有异常首先,那么你可以处理每一个不同的:

public function checkExists($bundle, $val) 
{ 
    try{ 
     $this->em->getRepository($bundle.':'.$val); 
    } catch (\Exception $e){ // \Exception is the global scope exception 
     if ($e instanceof MappingException || $e instanceof ORMException) { 
      return false; 
     } 
     throw $e; //Rethrow it if you can't handle it here. 
    } 

    return true; 
} 

Alternatevely有多个渔获:

public function checkExists($bundle, $val) 
{ 
    try{ 
     $this->em->getRepository($bundle.':'.$val); 
    } catch (MappingException $e){ 
     return false; 
    } catch (ORMException $e) { 
     return false; 
    } //Other exceptions are still unhandled. 

    return true; 
} 

如果”重新使用PHP 7.1 +,那么你也可以这样做:

public function checkExists($bundle, $val) 
{ 
    try{ 
     $this->em->getRepository($bundle.':'.$val); 
    } catch (MappingException | ORMException $e){ //Catch either MappingException or ORMException 
     return false; 
    } //Other exceptions are still unhandled. 

    return true; 
} 
1

创建异常听众并在那里处理它们。

class ExceptionListener 
{ 
    /** @var LoggerInterface */ 
    private $logger; 

    public function __construct(LoggerInterface $logger) 
    { 
     $this->logger = $logger; 
    } 

    public function onKernelException(GetResponseForExceptionEvent $event) 
    { 
     $e = $event->getException(); 
     if ($e instanceof ValidationException) { 
      $this->logger->notice('Validation error' , $e->getViolations()); 
     } elseif ($e instanceof DomainException) { 
      $this->logger->warning('Exception ' . get_class($e) , ['message' => $e->getMessage()]); 
      $event->setResponse(
      new JsonResponse(['error' => $this->translator->trans($e->getOutMessage())], 400) 
      ); 
     } else { 
      $event->setResponse(
       new JsonResponse(['error' => $this->translator->trans('http.internal_server_error')], 500) 
      ); 
     } 
    } 
} 

更新services.yml

app.exception_listener: 
    class: Application\Listeners\ExceptionListener 
    arguments: ['@domain.logger'] 
    tags: 
     - { name: kernel.event_listener, event: kernel.exception } 

进一步阅读有关监听器和事件https://symfony.com/doc/current/event_dispatcher.html

我不知道,你应该创建一个映射抛出的异常的情况下,当你的应用程序出货。