Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I handle Doctrine errors?

Tags:

symfony

I am using Doctrine's dbal service in my Symfony2 app.

I query a non-existent table, which throws an error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'log.requests_20130311' doesn't exist.

Symfony2 catches this before I can, even in a try-catch block. I don't want this to kill my application. How can I handle it?

like image 557
Sam Selikoff Avatar asked Mar 12 '13 22:03

Sam Selikoff


2 Answers

Per @Coussinsky's comment, you need to have a \ in front of your exception:

try {
    $result_set = $this->connection->query($sql);
} catch (\Exception $e) {
    return 0;
}
like image 160
Sam Selikoff Avatar answered Nov 18 '22 01:11

Sam Selikoff


Doctrines DBAL layer is a wrapper around PDO, so you should be able to do:

try {
    // Query your non-existent table
} catch (\PDOException $e) {
    // Deal with it without killing your app
}

http://symfony.com/doc/current/cookbook/doctrine/dbal.html

like image 23
Chris McKinnel Avatar answered Nov 18 '22 03:11

Chris McKinnel