Skip to content

Commit 9fdab2e

Browse files
committed
PHPLIB-570: Consult NamespaceNotFound error code in DropCollection
Also changes the try/catch to CommandException and allows the original command reply to be returned instead of a synthetic document (based on pre-3.2 server responses).
1 parent 35c1ac2 commit 9fdab2e

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/Operation/DropCollection.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace MongoDB\Operation;
1919

2020
use MongoDB\Driver\Command;
21-
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
21+
use MongoDB\Driver\Exception\CommandException;
2222
use MongoDB\Driver\Server;
2323
use MongoDB\Driver\Session;
2424
use MongoDB\Driver\WriteConcern;
@@ -38,6 +38,9 @@
3838
*/
3939
class DropCollection implements Executable
4040
{
41+
/** @var integer */
42+
private static $errorCodeNamespaceNotFound = 26;
43+
4144
/** @var string */
4245
private static $errorMessageNamespaceNotFound = 'ns not found';
4346

@@ -122,13 +125,13 @@ public function execute(Server $server)
122125

123126
try {
124127
$cursor = $server->executeWriteCommand($this->databaseName, $command, $this->createOptions());
125-
} catch (DriverRuntimeException $e) {
128+
} catch (CommandException $e) {
126129
/* The server may return an error if the collection does not exist.
127-
* Check for an error message (unfortunately, there isn't a code)
128-
* and NOP instead of throwing.
129-
*/
130-
if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
131-
return (object) ['ok' => 0, 'errmsg' => self::$errorMessageNamespaceNotFound];
130+
* Check for an error code (or message for pre-3.2 servers) and
131+
* return the command reply instead of throwing. */
132+
if ($e->getCode() === self::$errorCodeNamespaceNotFound ||
133+
$e->getMessage() === self::$errorMessageNamespaceNotFound) {
134+
return $e->getResultDocument();
132135
}
133136

134137
throw $e;

tests/Operation/DropCollectionFunctionalTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public function testDropExistingCollection()
3838
$this->assertEquals(1, $writeResult->getInsertedCount());
3939

4040
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
41-
$operation->execute($server);
41+
$commandResult = $operation->execute($server);
4242

43+
$this->assertCommandSucceeded($commandResult);
4344
$this->assertCollectionDoesNotExist($this->getCollectionName());
4445
}
4546

@@ -51,7 +52,11 @@ public function testDropNonexistentCollection()
5152
$this->assertCollectionDoesNotExist($this->getCollectionName());
5253

5354
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
54-
$operation->execute($this->getPrimaryServer());
55+
$commandResult = $operation->execute($this->getPrimaryServer());
56+
57+
$this->assertIsObject($commandResult);
58+
$this->assertObjectHasAttribute('ok', $commandResult);
59+
$this->assertEquals(0, $commandResult->ok);
5560
}
5661

5762
public function testSessionOption()

0 commit comments

Comments
 (0)