Просмотр исходного кода

Merge pull request #177 from alcaeus/use-correct-gridfs-exception

Use correct exception code in GridFS classes
Andreas 8 лет назад
Родитель
Сommit
b64a142e8b

+ 6 - 6
lib/Mongo/MongoGridFS.php

@@ -207,14 +207,14 @@ class MongoGridFS extends MongoCollection
         try {
             $file = $this->insertFile($record, $options);
         } catch (MongoException $e) {
-            throw new MongoGridFSException('Could not store file: '. $e->getMessage(), 0, $e);
+            throw new MongoGridFSException('Could not store file: '. $e->getMessage(), $e->getCode(), $e);
         }
 
         try {
             $this->insertChunksFromBytes($bytes, $file);
         } catch (MongoException $e) {
             $this->delete($file['_id']);
-            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
+            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), $e->getCode(), $e);
         }
 
         return $file['_id'];
@@ -257,14 +257,14 @@ class MongoGridFS extends MongoCollection
         try {
             $file = $this->insertFile($record, $options);
         } catch (MongoException $e) {
-            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
+            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), $e->getCode(), $e);
         }
 
         try {
             $length = $this->insertChunksFromFile($handle, $file, $md5);
         } catch (MongoException $e) {
             $this->delete($file['_id']);
-            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
+            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), $e->getCode(), $e);
         }
 
 
@@ -277,7 +277,7 @@ class MongoGridFS extends MongoCollection
             try {
                 $update['md5'] = $md5;
             } catch (MongoException $e) {
-                throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
+                throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), $e->getCode(), $e);
             }
         }
 
@@ -289,7 +289,7 @@ class MongoGridFS extends MongoCollection
                 }
             } catch (MongoException $e) {
                 $this->delete($file['_id']);
-                throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
+                throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), $e->getCode(), $e);
             }
         }
 

+ 5 - 2
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -133,6 +133,7 @@ class MongoCollectionTest extends TestCase
 
         $this->expectException(\MongoDuplicateKeyException::class);
         $this->expectExceptionMessageRegExp('/E11000 duplicate key error .* mongo-php-adapter\.test/');
+        $this->expectExceptionCode(11000);
         $collection->insert($document);
     }
 
@@ -235,11 +236,13 @@ class MongoCollectionTest extends TestCase
 
     public function testBatchInsertException()
     {
+        $id = new \MongoId();
+        $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
+
         $this->expectException(\MongoDuplicateKeyException::class);
         $this->expectExceptionMessageRegExp('/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
+        $this->expectExceptionCode(11000);
 
-        $id = new \MongoId();
-        $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
         $this->getCollection()->batchInsert($documents);
     }
 

+ 6 - 1
tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php

@@ -347,6 +347,7 @@ class MongoGridFSTest extends TestCase
 
         $this->expectException(\MongoGridFSException::class);
         $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->expectExceptionCode(11000);
 
         $collection->storeBytes('foo', ['_id' => $id]);
     }
@@ -361,6 +362,7 @@ class MongoGridFSTest extends TestCase
 
         $this->expectException(\MongoGridFSException::class);
         $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
+        $this->expectExceptionCode(11000);
 
         $collection->storeBytes('foo');
     }
@@ -375,6 +377,7 @@ class MongoGridFSTest extends TestCase
 
         $this->expectException(\MongoGridFSException::class);
         $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->expectExceptionCode(11000);
 
         $collection->storeFile(__FILE__, ['_id' => $id]);
     }
@@ -389,6 +392,7 @@ class MongoGridFSTest extends TestCase
 
         $this->expectException(\MongoGridFSException::class);
         $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
+        $this->expectExceptionCode(11000);
 
         $collection->storeFile(__FILE__);
     }
@@ -403,12 +407,13 @@ class MongoGridFSTest extends TestCase
 
         $this->expectException(\MongoGridFSException::class);
         $this->expectExceptionMessageRegExp('/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->expectExceptionCode(11000);
 
         $collection->storeFile(fopen(__FILE__, 'r'));
     }
 
     /**
-     * @var \MongoID
+     * @return \MongoID
      */
     protected function prepareFile($data = 'abcd', $extra = [])
     {