Explorar el Código

Ensure correct exceptions in GridFS class

Andreas Braun hace 10 años
padre
commit
9741e02412

+ 7 - 7
lib/Mongo/MongoGridFS.php

@@ -203,14 +203,14 @@ class MongoGridFS extends MongoCollection
         try {
             $file = $this->insertFile($record, $options);
         } catch (MongoException $e) {
-            throw new MongoGridFSException('Cannot insert file record', 0, $e);
+            throw new MongoGridFSException('Could not store file: '. $e->getMessage(), 0, $e);
         }
 
         try {
             $this->insertChunksFromBytes($bytes, $file);
         } catch (MongoException $e) {
             $this->delete($file['_id']);
-            throw new MongoGridFSException('Error while inserting chunks', 0, $e);
+            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
         }
 
         return $file['_id'];
@@ -253,14 +253,14 @@ class MongoGridFS extends MongoCollection
         try {
             $file = $this->insertFile($record, $options);
         } catch (MongoException $e) {
-            throw new MongoGridFSException('Cannot insert file record', 0, $e);
+            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
         }
 
         try {
             $length = $this->insertChunksFromFile($handle, $file, $md5);
         } catch (MongoException $e) {
             $this->delete($file['_id']);
-            throw new MongoGridFSException('Error while inserting chunks', 0, $e);
+            throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
         }
 
 
@@ -273,7 +273,7 @@ class MongoGridFS extends MongoCollection
             try {
                 $update['md5'] = $md5;
             } catch (MongoException $e) {
-                throw new MongoGridFSException('Error computing MD5 checksum', 0, $e);
+                throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
             }
         }
 
@@ -281,11 +281,11 @@ class MongoGridFS extends MongoCollection
             try {
                 $result = $this->update(['_id' => $file['_id']], ['$set' => $update]);
                 if (! $this->isOKResult($result)) {
-                    throw new MongoGridFSException('Error updating file record');
+                    throw new MongoGridFSException('Could not store file');
                 }
             } catch (MongoException $e) {
                 $this->delete($file['_id']);
-                throw new MongoGridFSException('Error updating file record', 0, $e);
+                throw new MongoGridFSException('Could not store file: ' . $e->getMessage(), 0, $e);
             }
 
         }

+ 1 - 1
tests/Alcaeus/MongoDbAdapter/MongoCollectionTest.php

@@ -91,7 +91,7 @@ class MongoCollectionTest extends TestCase
         $collection->insert($document);
 
         unset($document['_id']);
-        $this->setExpectedException('MongoDuplicateKeyException');
+        $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
         $collection->insert($document);
     }
 

+ 5 - 5
tests/Alcaeus/MongoDbAdapter/MongoGridFSTest.php

@@ -304,7 +304,7 @@ class MongoGridFSTest extends TestCase
         $document = ['_id' => $id];
         $collection->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file: localhost:27017: E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
 
         $collection->storeBytes('foo', ['_id' => $id]);
     }
@@ -317,7 +317,7 @@ class MongoGridFSTest extends TestCase
         $document = ['n' => 0];
         $collection->chunks->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file: localhost:27017: E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
+        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
 
         $collection->storeBytes('foo');
     }
@@ -330,7 +330,7 @@ class MongoGridFSTest extends TestCase
         $document = ['_id' => $id];
         $collection->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file: localhost:27017: E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
 
         $collection->storeFile(__FILE__, ['_id' => $id]);
     }
@@ -343,7 +343,7 @@ class MongoGridFSTest extends TestCase
         $document = ['n' => 0];
         $collection->chunks->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file: localhost:27017: E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
+        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.chunks/');
 
         $collection->storeFile(__FILE__);
     }
@@ -356,7 +356,7 @@ class MongoGridFSTest extends TestCase
         $document = ['length' => filesize(__FILE__)];
         $collection->insert($document);
 
-        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file: localhost:27017: E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
+        $this->setExpectedExceptionRegExp('MongoGridFSException', '/Could not store file:.* E11000 duplicate key error .* mongo-php-adapter\.fs\.files/');
 
         $collection->storeFile(fopen(__FILE__, 'r'));
     }