rdbms: make query exceptions include the backtrace when logged
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 13 Mar 2019 16:12:47 +0000 (09:12 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 13 Mar 2019 18:54:46 +0000 (18:54 +0000)
Previously, the corresponding Exception channel entry was relied on for
the backtrace, though there might not always be one.

Change-Id: I4a3b0fec0beaec1ebfe71be3850aa353cd37d562

includes/libs/rdbms/database/Database.php

index 49b2792..6d80f02 100644 (file)
@@ -1236,7 +1236,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                                        # option is ROLLBACK, since the snapshots would have been released.
                                        $this->trxStatus = self::STATUS_TRX_ERROR;
                                        $this->trxStatusCause =
-                                               $this->makeQueryException( $lastError, $lastErrno, $sql, $fname );
+                                               $this->getQueryExceptionAndLog( $lastError, $lastErrno, $sql, $fname );
                                        $tempIgnore = false; // cannot recover
                                        $this->trxStatusIgnoredCause = null;
                                }
@@ -1489,7 +1489,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                if ( $tempIgnore ) {
                        $this->queryLogger->debug( "SQL ERROR (ignored): $error\n" );
                } else {
-                       $exception = $this->makeQueryException( $error, $errno, $sql, $fname );
+                       $exception = $this->getQueryExceptionAndLog( $error, $errno, $sql, $fname );
 
                        throw $exception;
                }
@@ -1502,7 +1502,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
         * @param string $fname
         * @return DBError
         */
-       private function makeQueryException( $error, $errno, $sql, $fname ) {
+       private function getQueryExceptionAndLog( $error, $errno, $sql, $fname ) {
                $sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
                $this->queryLogger->error(
                        "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
@@ -1512,6 +1512,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                                'error' => $error,
                                'sql1line' => $sql1line,
                                'fname' => $fname,
+                               'trace' => ( new RuntimeException() )->getTraceAsString()
                        ] )
                );
                $this->queryLogger->debug( "SQL ERROR: " . $error . "\n" );