Merge pull request #8650 from annando/annando/issue8572

Issue 8572: Ensure to log database errors
This commit is contained in:
Hypolite Petovan 2020-05-17 09:10:19 -04:00 committed by GitHub
commit 27fc2209b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -504,6 +504,7 @@ class Database
$sql = "/*" . System::callstack() . " */ " . $sql; $sql = "/*" . System::callstack() . " */ " . $sql;
} }
$is_error = false;
$this->error = ''; $this->error = '';
$this->errorno = 0; $this->errorno = 0;
$this->affected_rows = 0; $this->affected_rows = 0;
@ -533,6 +534,7 @@ class Database
$this->error = $errorInfo[2]; $this->error = $errorInfo[2];
$this->errorno = $errorInfo[1]; $this->errorno = $errorInfo[1];
$retval = false; $retval = false;
$is_error = true;
break; break;
} }
$this->affected_rows = $retval->rowCount(); $this->affected_rows = $retval->rowCount();
@ -545,6 +547,7 @@ class Database
$this->error = $errorInfo[2]; $this->error = $errorInfo[2];
$this->errorno = $errorInfo[1]; $this->errorno = $errorInfo[1];
$retval = false; $retval = false;
$is_error = true;
break; break;
} }
@ -562,6 +565,7 @@ class Database
$this->error = $errorInfo[2]; $this->error = $errorInfo[2];
$this->errorno = $errorInfo[1]; $this->errorno = $errorInfo[1];
$retval = false; $retval = false;
$is_error = true;
} else { } else {
$retval = $stmt; $retval = $stmt;
$this->affected_rows = $retval->rowCount(); $this->affected_rows = $retval->rowCount();
@ -580,6 +584,7 @@ class Database
$this->error = $this->connection->error; $this->error = $this->connection->error;
$this->errorno = $this->connection->errno; $this->errorno = $this->connection->errno;
$retval = false; $retval = false;
$is_error = true;
} else { } else {
if (isset($retval->num_rows)) { if (isset($retval->num_rows)) {
$this->affected_rows = $retval->num_rows; $this->affected_rows = $retval->num_rows;
@ -596,6 +601,7 @@ class Database
$this->error = $stmt->error; $this->error = $stmt->error;
$this->errorno = $stmt->errno; $this->errorno = $stmt->errno;
$retval = false; $retval = false;
$is_error = true;
break; break;
} }
@ -623,6 +629,7 @@ class Database
$this->error = $this->connection->error; $this->error = $this->connection->error;
$this->errorno = $this->connection->errno; $this->errorno = $this->connection->errno;
$retval = false; $retval = false;
$is_error = true;
} else { } else {
$stmt->store_result(); $stmt->store_result();
$retval = $stmt; $retval = $stmt;
@ -631,6 +638,16 @@ class Database
break; break;
} }
// See issue https://github.com/friendica/friendica/issues/8572
// Ensure that we always get an error message on an error.
if ($is_error && empty($this->errorno)) {
$this->errorno = -1;
}
if ($is_error && empty($this->error)) {
$this->error = 'Unknown database error';
}
// We are having an own error logging in the function "e" // We are having an own error logging in the function "e"
if (($this->errorno != 0) && !$called_from_e) { if (($this->errorno != 0) && !$called_from_e) {
// We have to preserve the error code, somewhere in the logging it get lost // We have to preserve the error code, somewhere in the logging it get lost
@ -642,8 +659,8 @@ class Database
} }
$this->logger->error('DB Error', [ $this->logger->error('DB Error', [
'code' => $this->errorno, 'code' => $errorno,
'error' => $this->error, 'error' => $error,
'callstack' => System::callstack(8), 'callstack' => System::callstack(8),
'params' => $this->replaceParameters($sql, $args), 'params' => $this->replaceParameters($sql, $args),
]); ]);
@ -654,21 +671,21 @@ class Database
// It doesn't make sense to continue when the database connection was lost // It doesn't make sense to continue when the database connection was lost
if ($this->in_retrial) { if ($this->in_retrial) {
$this->logger->notice('Giving up retrial because of database error', [ $this->logger->notice('Giving up retrial because of database error', [
'code' => $this->errorno, 'code' => $errorno,
'error' => $this->error, 'error' => $error,
]); ]);
} else { } else {
$this->logger->notice('Couldn\'t reconnect after database error', [ $this->logger->notice('Couldn\'t reconnect after database error', [
'code' => $this->errorno, 'code' => $errorno,
'error' => $this->error, 'error' => $error,
]); ]);
} }
exit(1); exit(1);
} else { } else {
// We try it again // We try it again
$this->logger->notice('Reconnected after database error', [ $this->logger->notice('Reconnected after database error', [
'code' => $this->errorno, 'code' => $errorno,
'error' => $this->error, 'error' => $error,
]); ]);
$this->in_retrial = true; $this->in_retrial = true;
$ret = $this->p($sql, $args); $ret = $this->p($sql, $args);
@ -745,8 +762,8 @@ class Database
} }
$this->logger->error('DB Error', [ $this->logger->error('DB Error', [
'code' => $this->errorno, 'code' => $errorno,
'error' => $this->error, 'error' => $error,
'callstack' => System::callstack(8), 'callstack' => System::callstack(8),
'params' => $this->replaceParameters($sql, $params), 'params' => $this->replaceParameters($sql, $params),
]); ]);
@ -755,8 +772,8 @@ class Database
// A reconnect like in $this->p could be dangerous with modifications // A reconnect like in $this->p could be dangerous with modifications
if ($errorno == 2006) { if ($errorno == 2006) {
$this->logger->notice('Giving up because of database error', [ $this->logger->notice('Giving up because of database error', [
'code' => $this->errorno, 'code' => $errorno,
'error' => $this->error, 'error' => $error,
]); ]);
exit(1); exit(1);
} }