0

I want to catch PDOException in symfony 2.6, especially ConnectionException.

For instance if I stop my MySQL server I want to catch that exception and return a customised message to the user, but it seem that it's uncatchable in customised kernel.exception listner, and either in try catch block, i don't know if it's a symfony problem or something must be done.

I also tried out to customise error page like said in documentation but usselssly, I seached in web for solution, but I found nothing except something about redefining a controller in frameworkbundle whish is responsable of converting Exception into error page.

But I really don't want to go for that solution since i'm new with symfony.

felipsmartins
  • 13,269
  • 4
  • 48
  • 56

1 Answers1

1

You can do this by creating an exception listener and catch Pdo exception :

service.yml:

kernel.listener.your_pdo_listener:
        class: Acme\AppBundle\EventListener\YourExceptionListener
        tags:
           - { name: kernel.event_listener, event: kernel.exception, method: onPdoException }

Then the listener class :

YourExceptionListener

UPDATED

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class YourExceptionListener
{
     public function onPdoException(GetResponseForExceptionEvent $event)
     {
          $exception = $event->getException();

          if ($exception instanceof \PDOException || $exception->getPrevious() instanceof \PDOException) {
              //now you can do whatever you want with this exception
          }
     }
}

Snippets from : Catching database exceptions in Symfony2

I have done more test, so the test i made first was making a query to the database, that's why i got pdoexception as first exception but sometime it can be a twig exception as you know twig throw runtime exception if it couldn't contact database but hopefully we can get the previous exception also and this can work with other exceptions which can be thrown after the PDO ones, so hopefully it will work for you as expected so i edited the code to check if previous exception is a PDOException also.

Community
  • 1
  • 1
Nawfal Serrar
  • 2,213
  • 1
  • 14
  • 22
  • i already saw this solution but it didn't work, normaly the exception listener according to the documentation should be able to listen to all kind of exception even if you don't do the test, the method name has no obligation (onPdoexception or onkernelexception), besides i'm able to catch other exception – Yassine Ah May 19 '15 at 17:00
  • Well, that's odd because i have the same configuration on my dev as i showed you for the listener and its catching PDOExceptions , tested with changing database name to a none existent database i catched that exception. Could not stop the server to test but i believe it would be the same result. – Nawfal Serrar May 20 '15 at 02:45
  • i tried out this too, but i still not able to cath it – Yassine Ah May 21 '15 at 13:26
  • @YassineAh: Are you sure that you tagged your service properly? Nawfal's example should work... – Jovan Perovic May 21 '15 at 13:42
  • yeah i'm able to catch the other exceptions – Yassine Ah May 21 '15 at 14:26
  • This works for me mate i have no clue why not you, tested it shuting down server catched, even child pdoexception with the added check for previous so your problem must be somewhere else or the exception you are looking for is not pdoexception – Nawfal Serrar May 21 '15 at 16:33
  • @Nawfal Serrar: are you using this name space : use Doctrine\DBAL\PDOException; ? in your customised exception listener, and also i want to ask you if you don't catch a psoexception, in the production envirnement do you get a error page with http satut 500 or do you get the stack trace of the pdo exception like i do,and thanks for your answers. – Yassine Ah May 21 '15 at 18:14
  • This one doesnt have namespace its part of the Global namespace either use \PDOException as PDOException; or directly use \ in the check, if instanceof \PDOException – Nawfal Serrar May 22 '15 at 02:21
  • I think that's why it didn't work for you, i don't think there is a Doctrine\DBAL\PDOException in that namespace so you would never catched it but now i guess i should work – Nawfal Serrar May 22 '15 at 02:23