Skip to content

Commit 46d97e2

Browse files
Improve error handling in updateDocuments to ensure exceptions are properly thrown or handled
1 parent 5310a9a commit 46d97e2

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/Database/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4456,7 +4456,7 @@ public function updateDocuments(
44564456
try {
44574457
$onNext && $onNext($doc);
44584458
} catch (Exception $e) {
4459-
$onError && $onError($e);
4459+
$onError ? $onError($e) : throw $e;
44604460
}
44614461
$modified++;
44624462
}

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3550,6 +3550,23 @@ public function testUpdateDocuments(): void
35503550

35513551
// Test Update half of the documents
35523552
$results = [];
3553+
try {
3554+
$count = $database->updateDocuments($collection, new Document([
3555+
'string' => 'text📝 updated',
3556+
]), [
3557+
Query::lessThan('integer', -1),
3558+
], onNext: function ($doc) use (&$results) {
3559+
$results[] = $doc;
3560+
throw new Exception("Error thrown to test update doesn't stopped and error is caught");
3561+
});
3562+
} catch (Exception $e) {
3563+
if ($e instanceof Exception) {
3564+
$this->assertInstanceOf(Exception::class, $e);
3565+
$this->assertEquals("Error thrown to test that deletion doesn't stop and error is caught", $e->getMessage());
3566+
} else {
3567+
$this->fail("Caught value is not an Exception.");
3568+
}
3569+
}
35533570
$count = $database->updateDocuments($collection, new Document([
35543571
'string' => 'text📝 updated',
35553572
]), [

0 commit comments

Comments
 (0)