Skip to content

Commit 3c5f799

Browse files
icewind1991backportbot[bot]
authored andcommitted
feat: add additional logging for database errors
including the stack trace of the current database transaction Signed-off-by: Robin Appelman <robin@icewind.nl> [skip ci]
1 parent 4a4f90c commit 3c5f799

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/private/DB/Connection.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class Connection extends \Doctrine\DBAL\Connection {
7979
/** @var DbDataCollector|null */
8080
protected $dbDataCollector = null;
8181

82+
protected bool $logDbException = false;
83+
private ?array $transactionBacktrace = null;
84+
8285
protected bool $logRequestId;
8386
protected string $requestId;
8487

@@ -110,6 +113,7 @@ public function __construct(
110113
$this->logger = \OC::$server->get(LoggerInterface::class);
111114

112115
$this->logRequestId = $this->systemConfig->getValue('db.log_request_id', false);
116+
$this->logDbException = $this->systemConfig->getValue('db.log_exceptions', false);
113117
$this->requestId = Server::get(IRequestId::class)->getId();
114118

115119
/** @var \OCP\Profiler\IProfiler */
@@ -263,7 +267,12 @@ public function executeQuery(string $sql, array $params = [], $types = [], Query
263267
$sql = $this->finishQuery($sql);
264268
$this->queriesExecuted++;
265269
$this->logQueryToFile($sql);
266-
return parent::executeQuery($sql, $params, $types, $qcp);
270+
try {
271+
return parent::executeQuery($sql, $params, $types, $qcp);
272+
} catch (\Exception $e) {
273+
$this->logDatabaseException($e);
274+
throw $e;
275+
}
267276
}
268277

269278
/**
@@ -294,7 +303,12 @@ public function executeStatement($sql, array $params = [], array $types = []): i
294303
$sql = $this->finishQuery($sql);
295304
$this->queriesExecuted++;
296305
$this->logQueryToFile($sql);
297-
return (int)parent::executeStatement($sql, $params, $types);
306+
try {
307+
return (int)parent::executeStatement($sql, $params, $types);
308+
} catch (\Exception $e) {
309+
$this->logDatabaseException($e);
310+
throw $e;
311+
}
298312
}
299313

300314
protected function logQueryToFile(string $sql): void {
@@ -360,7 +374,12 @@ public function insertIfNotExist($table, $input, array $compare = null) {
360374
}
361375

362376
public function insertIgnoreConflict(string $table, array $values) : int {
363-
return $this->adapter->insertIgnoreConflict($table, $values);
377+
try {
378+
return $this->adapter->insertIgnoreConflict($table, $values);
379+
} catch (\Exception $e) {
380+
$this->logDatabaseException($e);
381+
throw $e;
382+
}
364383
}
365384

366385
private function getType($value) {

lib/private/DB/ConnectionAdapter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,8 @@ public function getDatabaseProvider(): string {
261261
throw new \Exception('Database ' . $platform::class . ' not supported');
262262
}
263263
}
264+
265+
public function logDatabaseException(\Exception $exception) {
266+
$this->inner->logDatabaseException($exception);
267+
}
264268
}

0 commit comments

Comments
 (0)