Przeglądaj źródła

Add method to convert exceptions to result array

Andreas Braun 10 lat temu
rodzic
commit
edae7675cd

+ 15 - 0
lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php

@@ -82,4 +82,19 @@ class ExceptionConverter
 
         return new $class($message, $code, $e);
     }
+
+    /**
+     * Converts an exception to
+     *
+     * @param Exception\Exception $e
+     * @return array
+     */
+    public static function toResultArray(Exception\Exception $e)
+    {
+        return [
+            'ok' => 0.0,
+            'errmsg' => $e->getMessage(),
+            'code' => $e->getCode(),
+        ];
+    }
 }

+ 1 - 6
lib/Mongo/MongoCollection.php

@@ -655,12 +655,7 @@ class MongoCollection
         try {
             return TypeConverter::toLegacy($this->collection->dropIndex($indexName));
         } catch (\MongoDB\Driver\Exception\Exception $e) {
-            return [
-                'nIndexesWas' => count($this->getIndexInfo()),
-                'errmsg' => $e->getMessage(),
-                'ok' => 0.0,
-                'code' => $e->getCode(),
-            ];
+            return ExceptionConverter::toResultArray($e) + ['nIndexesWas' => count($this->getIndexInfo())];
         }
     }
 

+ 1 - 5
lib/Mongo/MongoDB.php

@@ -404,11 +404,7 @@ class MongoDB
 
             return iterator_to_array($cursor)[0];
         } catch (\MongoDB\Driver\Exception\Exception $e) {
-            return [
-                'ok' => 0.0,
-                'errmsg' => $e->getMessage(),
-                'code' => $e->getCode(),
-            ];
+            return ExceptionConverter::toResultArray($e);
         }
     }