Skip to content

Commit 0be3912

Browse files
committed
Allow save debug data before throw exceptions
1 parent 2f0768f commit 0be3912

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/Database.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,14 +776,26 @@ public function setDebugCollector(DatabaseCollector $collector) : static
776776
protected function addToDebug(Closure $function) : mixed
777777
{
778778
$start = \microtime(true);
779-
$result = $function();
779+
try {
780+
$result = $function();
781+
} catch (Exception $exception) {
782+
$this->finalizeAddToDebug($start);
783+
throw $exception;
784+
}
785+
$this->finalizeAddToDebug($start);
786+
return $result;
787+
}
788+
789+
protected function finalizeAddToDebug(float $start) : void
790+
{
780791
$end = \microtime(true);
792+
$rows = $this->mysqli->affected_rows;
793+
$rows = $rows < 0 ? $rows . ' (failed)' : $rows;
781794
$this->debugCollector->addData([
782795
'start' => $start,
783796
'end' => $end,
784797
'statement' => $this->lastQuery(),
785-
'rows' => $this->mysqli->affected_rows,
798+
'rows' => $rows,
786799
]);
787-
return $result;
788800
}
789801
}

tests/Debug/DatabaseCollectorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,16 @@ protected function getHostInfo() : string
120120
self::assertStringContainsString('Socket:', $collector->getContents());
121121
self::assertStringNotContainsString('Port:', $collector->getContents());
122122
}
123+
124+
public function testFinalizeAddToDebug() : void
125+
{
126+
try {
127+
$this->makeDatabase()->query('Foo Bar');
128+
} catch (\Exception) {
129+
}
130+
$data = $this->collector->getData();
131+
$data = $data[\array_key_last($data)];
132+
self::assertSame('Foo Bar', $data['statement']);
133+
self::assertSame('-1 (failed)', $data['rows']);
134+
}
123135
}

0 commit comments

Comments
 (0)