Sfoglia il codice sorgente

Properly catch exceptions when deleting indexes

Andreas Braun 10 anni fa
parent
commit
5bb37a15e6

+ 5 - 1
lib/Mongo/MongoCollection.php

@@ -667,7 +667,11 @@ class MongoCollection
      */
     public function deleteIndexes()
     {
-        return TypeConverter::toLegacy($this->collection->dropIndexes());
+        try {
+            return TypeConverter::toLegacy($this->collection->dropIndexes());
+        } catch (\MongoDB\Driver\Exception\Exception $e) {
+            return ExceptionConverter::toResultArray($e);
+        }
     }
 
     /**

+ 10 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -928,6 +928,16 @@ class MongoCollectionTest extends TestCase
         $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
     }
 
+    public function testDeleteIndexesForNonExistingCollection()
+    {
+        $expected = [
+            'ok' => 0.0,
+            'errmsg' => 'ns not found',
+            'code' => 26,
+        ];
+        $this->assertSame($expected, $this->getcollection('nonExisting')->deleteIndexes());
+    }
+
     public function testGetIndexInfo()
     {
         $collection = $this->getCollection();