International PHP Conference Munich 2025

MongoDB\Driver\Exception\BulkWriteCommandException::getErrorReply

(mongodb >=2.1.0)

MongoDB\Driver\Exception\BulkWriteCommandException::getErrorReplyRenvoie une erreur de commande de niveau supérieur

Description

final public MongoDB\Driver\Exception\BulkWriteCommandException::getErrorReply(): ?MongoDB\BSON\Document

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Renvoie une erreur de commande de niveau supérieur qui s'est produite lors de l'essaie de communication avec le serveur ou d'exécution de l'écriture en masse. Cette valeur peut être null si l'exception a été lancée en raison d'erreurs survenant sur des écritures individuelles.

Exemples

Exemple #1 Exemple de MongoDB\Driver\Exception\BulkWriteCommandException::getErrorReply()

<?php

$manager
= new MongoDB\Driver\Manager;

// Cet exemple utilise configureFailPoint pour simuler une erreur de commande de niveau supérieur
$manager->executeCommand('admin', new MongoDB\Driver\Command([
'configureFailPoint' => 'failCommand',
'mode' => ['times' => 1],
'data' => [
'failCommands' => ['bulkWrite'],
'errorCode' => 8, /* UnknownError */
],
]));

$bulk = new MongoDB\Driver\BulkWriteCommand;
$bulk->insertOne('db.coll', ['x' => 1]);

try {
$result = $manager->executeBulkWriteCommand($bulk);
} catch (
MongoDB\Driver\Exception\BulkWriteCommandException $e) {
var_dump($e->getErrorReply()?->toPHP());
}

?>

Résultat de l'exemple ci-dessus est similaire à :

object(stdClass)#12 (6) {
  ["ok"]=>
  float(0)
  ["errmsg"]=>
  string(43) "Failing command via 'failCommand' failpoint"
  ["code"]=>
  int(8)
  ["codeName"]=>
  string(12) "UnknownError"
  ["$clusterTime"]=>
  object(stdClass)#10 (2) {
    ["clusterTime"]=>
    object(MongoDB\BSON\Timestamp)#6 (2) {
      ["increment"]=>
      string(1) "7"
      ["timestamp"]=>
      string(10) "1744319389"
    }
    ["signature"]=>
    object(stdClass)#9 (2) {
      ["hash"]=>
      object(MongoDB\BSON\Binary)#7 (2) {
        ["data"]=>
        string(20) ""
        ["type"]=>
        int(0)
      }
      ["keyId"]=>
      object(MongoDB\BSON\Int64)#8 (1) {
        ["integer"]=>
        string(1) "0"
      }
    }
  }
  ["operationTime"]=>
  object(MongoDB\BSON\Timestamp)#11 (2) {
    ["increment"]=>
    string(1) "7"
    ["timestamp"]=>
    string(10) "1744319389"
  }
}

Voir aussi

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top