Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1634,8 +1634,12 @@ private function parseResponse(string $response, int $responseLength): stdClass|

// Check for write errors (duplicate key, etc.)
if (\property_exists($result, 'writeErrors') && !empty($result->writeErrors)) {
throw new Exception(
throw new BulkWriteException(
$result->writeErrors[0]->errmsg,
[
'writeErrors' => $result->writeErrors,
'nInserted' => $result->n ?? 0,
],
$result->writeErrors[0]->code
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class BulkWriteException extends Exception

public function __construct(string $message, array $result, int $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
parent::__construct($message, $code, $previous, [], $result['writeErrors'] ?? null);
$this->result = $result;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/MongoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MongoDB\BSON\ObjectId;
use PHPUnit\Framework\TestCase;
use Utopia\Mongo\Client;
use Utopia\Mongo\BulkWriteException;
use Utopia\Mongo\Exception;

class MongoTest extends TestCase
Expand Down Expand Up @@ -477,4 +478,44 @@ public function testCountMethod()
$this->getDatabase()->dropCollection($collectionName);
}
}

public function testInsertManyDuplicateThrowsBulkWriteException(): void
{
$collectionName = 'test_bulk_write_exception';
$this->getDatabase()->createCollection($collectionName);

try {
// Insert a doc with explicit _id
$this->getDatabase()->insert($collectionName, [
'_id' => 'dup_id',
'name' => 'Original',
]);

// insertMany with a duplicate _id should throw BulkWriteException
try {
$this->getDatabase()->insertMany($collectionName, [
['_id' => 'dup_id', 'name' => 'Duplicate'],
['_id' => 'new_id', 'name' => 'New'],
], ['ordered' => false]);

self::fail('Expected BulkWriteException');
} catch (BulkWriteException $e) {
// Should be a duplicate key error
self::assertSame(11000, $e->getCode());

$result = $e->getResult();
self::assertArrayHasKey('nInserted', $result);
self::assertArrayHasKey('writeErrors', $result);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Weak assertion on nInserted count

The assertion assertGreaterThanOrEqual(1, $result['nInserted']) would pass even if nInserted is 100. Since the setup inserts exactly two documents — one duplicate that must fail and one new one that must succeed — the count should be precisely 1 with ordered: false.

Suggested change
self::assertArrayHasKey('writeErrors', $result);
self::assertSame(1, $result['nInserted']);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both fixed in 56fe485:

  • nInserted assertion tightened to assertSame(1, ...)
  • BulkWriteException now passes writeErrors to parent constructor, so getWriteErrors() is consistent with getResult()['writeErrors']

self::assertSame(1, $result['nInserted']);
}

// 'new_id' should have been inserted despite the duplicate (ordered: false)
$found = $this->getDatabase()->find($collectionName, ['_id' => 'new_id']);
$docs = $found->cursor->firstBatch ?? [];
self::assertCount(1, $docs);
self::assertSame('New', $docs[0]->name);
} finally {
$this->getDatabase()->dropCollection($collectionName);
}
}
}
Loading