Skip to content

Conversation

@thekid
Copy link
Member

@thekid thekid commented Jul 19, 2025

This pull request changes update() and upsert() to raise exceptions when write errors occur during the following:

use com\mongodb\MongoConnection;

$c= new MongoConnection($dsn);
$c->collection('test.entries')->upsert(['test' => true], [
  '$inc' => ['version' => 1],
  '$setOnInsert' => ['version' => 1],
]);

This yields the following in the result:

writeErrors => [[
  index => 0
  code => 40
  errmsg => "Updating the path 'version' would create a conflict at 'version'"
]]

👉 The methods now throw a com.mongodb.WriteErrors exception which extends the com.mongodb.Error class.

Accessing the errors

use com\mongodb\{MongoConnection, WriteErrors};
use util\cmd\Console;

$c= new MongoConnection($dsn);
try {
  $c->collection('test.entries')->upsert(['test' => true], [
    '$inc' => ['version' => 1],
    '$setOnInsert' => ['version' => 1],
  ]);
} catch (WriteErrors $errors) {
  Console::writeLine('The following errors occurred: ', $errors->list());
}

See also

@thekid thekid merged commit 5173116 into master Jul 19, 2025
12 checks passed
@thekid thekid deleted the feature/write-errors branch July 19, 2025 09:05
@thekid
Copy link
Member Author

thekid commented Jul 19, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants