繁体   English   中英

为什么 PDO 抛出 ErrorException 而不是 PDOException?

[英]Why is PDO throwing an ErrorException rather than a PDOException?

我让 Sentry 跟踪我的 PHP 应用程序中未捕获的异常,并且我注意到来自 PDO 的一个特殊的未捕获异常。 代码如下所示:

   /**
    * @return boolean TRUE if the connection to the database worked; FALSE otherwise. 
    */
   public function verifyDatabase() {
       try{ 
           $this->pdo->query('SELECT 1');
           return true;
       }
       catch (\PDOException $e) {
           echo 'Lost connection to database: ' . $e->getMessage() . PHP_EOL;
           return false;
       }
    }

这应该会捕获诸如“MySQL 服务器已消失”之类的错误,并且它确实可以在我的开发机器上运行。 但是,Sentry 最近记录了这个错误:

ErrorException PDO::query(): MySQL 服务器已经消失

根据哨兵的说法,这是由$this->pdo->query('SELECT 1');抛出的$this->pdo->query('SELECT 1'); 上面的声明。 像这样的错误应该被try/catch 为什么 PDO 抛出ErrorException而不是PDOException

我无法重现 ErrorException。

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

sleep(20); // during this sleep, I stop my MySQL Server instance.

$result = $pdo->query("SELECT 1");

输出:

Warning: PDO::query(): MySQL server has gone away

Warning: PDO::query(): Error reading result set's header

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Stack trace:
#0 /Users/bkarwin/Documents/SO/pdo.php(8): PDO->query('SELECT 1')

这表明它在服务器消失时抛出 PDOException,而不是 ErrorException。

使用 MySQL 5.6.37 和 PHP 7.1.23 进行测试。

我想知道您在问题中显示的代码是否实际上是部署并向 Sentry 抛出异常的代码。 也许您有一些代码,例如:

   catch (\PDOException $e) {
       throw ErrorException($e->getMessage());
   }

在您的 verifyDatabase() 函数中,或者在调用 verifyDatabase() 的代码中。 换句话说,当 verifyDatabase() 返回false时,您的应用会做什么?

好吧,我想我已经想通了。 这似乎与 PDO MySQL 驱动程序发出警告的错误有关,即使它们应该被禁用(另请参阅:此答案)。 我相信 Sentry 会将这些视为错误。

通过修改 Bill Karwin 的测试脚本,我终于能够复制并解决这个问题:

// Initialize the Sentry reporting client
$ravenClient = new \Raven_Client(SENTRY_KEY);
$ravenClient->install();

echo 'Connecting...' . PHP_EOL;

$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo 'Connected. Waiting...' . PHP_EOL;

sleep(20); // during this sleep, I stop my MySQL Server instance.

echo 'Querying...' . PHP_EOL;

try {
    $result = $pdo->query("SELECT 1");
}
catch(\PDOException $e) {
    echo 'Caught PDOException ' . $e->getMessage() . PHP_EOL;
}

这将打印以下内容:

Connecting...
Connected. Waiting...
Querying...
PHP Warning:  PDO::query(): MySQL server has gone away in /home/xxx/src/test.php on line 37
PHP Stack trace:
PHP   1. {main}() /home/xxx/src/test.php:0
PHP   2. PDO->query() /home/xxx/src/test.php:37
Caught PDOException SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Done.

Sentry 将在query()行上记录一个ErrorException

我能够通过实施jferrerPHP 错误报告上发布的“解决方案”来解决这个问题。

// Convert NOTICE, WARNING, ... in Exceptions
$convertErrorToException = function ($level, $message, $file, $line){
    throw new ErrorException($message, 0, $level, $file, $line);
};

// The $previousErrorHandler will contain Sentry's handler
$previousErrorHandler = set_error_handler($convertErrorToException);

try {
    $result = $pdo->query("SELECT 1");
}
catch(\PDOException $e) {
    echo 'Caught PDOException ' . $e->getMessage() . PHP_EOL;
}
catch(\ErrorException $e) {
    echo 'Caught ErrorException ' . $e->getMessage() . PHP_EOL;
}

// Restore Sentry as the default handler
set_error_handler($previousErrorHandler);

这导致仅抛出并捕获 ErrorException:

Connecting...
Connected. Waiting...
Querying...
Caught ErrorException PDO::query(): MySQL server has gone away
Done.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM