Skip to content

Commit 9a8756a

Browse files
Improve error handling in document deletion and add test for exception catching
1 parent 852e3d3 commit 9a8756a

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/Database/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5893,7 +5893,7 @@ public function deleteDocuments(
58935893
try {
58945894
$onNext && $onNext($document);
58955895
} catch (Exception $e) {
5896-
$onError && $onError($e);
5896+
$onError ? $onError($e) : throw $e;
58975897
}
58985898
$modified++;
58995899
}

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3953,6 +3953,31 @@ public function testDeleteBulkDocuments(): void
39533953
*/
39543954
$selects = ['$sequence', '$id', '$collection', '$permissions', '$updatedAt'];
39553955

3956+
try {
3957+
// a non existent document to test the error thrown
3958+
$database->deleteDocuments(
3959+
collection: 'bulk_delete',
3960+
queries: [
3961+
Query::select([...$selects, '$createdAt']),
3962+
Query::lessThan('$createdAt', '1800-01-01'),
3963+
Query::orderAsc('$createdAt'),
3964+
Query::orderAsc(),
3965+
Query::limit(1),
3966+
],
3967+
batchSize: 1,
3968+
onNext: function () {
3969+
throw new Exception("Error thrown to test that deletion doesn't stop and error is caught");
3970+
}
3971+
);
3972+
} catch (Exception $e) {
3973+
if ($e instanceof Exception) {
3974+
$this->assertInstanceOf(Exception::class, $e);
3975+
$this->assertEquals("Error thrown to test that deletion doesn't stop and error is caught", $e->getMessage());
3976+
} else {
3977+
$this->fail("Caught value is not an Exception.");
3978+
}
3979+
}
3980+
39563981
$count = $database->deleteDocuments(
39573982
collection: 'bulk_delete',
39583983
queries: [

0 commit comments

Comments
 (0)