Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO::ERRMODE_EXCEPTION doesn't suppress warning

Tags:

php

mysql

pdo

http://php.net/manual/en/pdo.error-handling.php

PDO::ERRMODE_WARNING

In addition to setting the error code, PDO will emit a traditional E_WARNING message. This setting is useful during debugging/testing, if you just want to see what problems occurred without interrupting the flow of the application.

PDO::ERRMODE_EXCEPTION

In addition to setting the error code, PDO will throw a PDOException and set its properties to reflect the error code and error information. This setting is also useful during debugging, as it will effectively "blow up" the script at the point of the error, very quickly pointing a finger at potential problem areas in your code (remember: transactions are automatically rolled back if the exception causes the script to terminate).

Exception mode is also useful because you can structure your error handling more clearly than with traditional PHP-style warnings, and with less code/nesting than by running in silent mode and explicitly checking the return value of each database call.

However, the code:

$connection = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', '***');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$connection->query('SET wait_timeout=1;');
sleep(2);

try {
    $connection->query('SELECT 1;');
} catch (\Exception $e) {
    echo sprintf('Caught %s exception: %s', get_class($e), $e->getMessage()) . PHP_EOL;
}

triggers warnings:

PHP Warning:  PDO::query(): MySQL server has gone away in pdo.php on line 13
PHP Warning:  PDO::query(): Error reading result set's header in pdo.php on line 13
Caught PDOException exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

IMPORTANT: The question is not about problem with MySQL server gone, but about PDO error handling.

UPDATE: Warnings triggered in all three modes: ERRMODE_SILENT, ERRMODE_WARNING, ERRMODE_EXCEPTION

PHP 7.2.1 (cli) (built: Jan  5 2018 17:34:14) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
like image 733
Sergei Kasatkin Avatar asked Feb 08 '18 10:02

Sergei Kasatkin


People also ask

What is PDO :: Errmode_exception?

PDO::ERRMODE_EXCEPTION 0, this is the default mode. In addition to setting the error code, PDO will throw a PDOException and set its properties to reflect the error code and error information.


1 Answers

I'd dare say it's a bug. I found two relevant tickets:

  • Bug #63812: PDO Triggers Warning(s) Regardless of Error Handling Strategy, filed on 2012 for PHP/5.3.19
  • Bug #74401: PDO trigger warning already set throw exception, filed on 2017 for PHP/7.0.17

In any case, they're still open and it isn't entirely clear whether they're valid issues (though I suspect they are). It doesn't seem to be a design decision because other MySQL errors do not trigger both, warning and exception:

$connection = new PDO('mysql:host=127.0.0.1;dbname=test', 'test', 'test',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING]);
$connection->query('SELECT * FROM foo');

Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.foo' doesn't exist

$connection = new PDO('mysql:host=127.0.0.1;dbname=test', 'test', 'test',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$connection->query('SELECT * FROM foo');

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.foo' doesn't exist'

like image 126
Álvaro González Avatar answered Sep 23 '22 02:09

Álvaro González