Catch different types of exceptions

I have a very simple function to check if an entity exists in a package:

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

    return true;
}

      

So, I have the following cases:

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)

      

So I see that the problem is that different exceptions are thrown, but how can I return false for any of the cases where one part does not exist? Is there a "generic" way to catch exceptions in symfony since catch (Exception $e)

s use Symfony\Component\Config\Definition\Exception\Exception;

doesn't catch it.

+3


source to share


2 answers


There are several things to do: First, you can catch all exceptions, then you can handle each one differently:

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

      

Alternatively, they have several catches:



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

      

If you are using PHP 7.1+ you can also:

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

      

+3


source


Create an exception listener and handle them there.

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

      

Update services.yml



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

      

Further reading about listeners and events https://symfony.com/doc/current/event_dispatcher.html

I'm not sure if you should be creating situations that throw Mapping exceptions when your app is submitted.

+1


source







All Articles