Kaynağa Gözat

Implement ExceptionConverter

Olivier Lechevalier 10 yıl önce
ebeveyn
işleme
f7d4aad03a

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

@@ -0,0 +1,65 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+namespace Alcaeus\MongoDbAdapter;
+
+use MongoDB\Driver\Exception;
+
+/**
+ * @internal
+ */
+class ExceptionConverter
+{
+    /**
+     * @return \MongoException
+     */
+    public static function convertException(Exception\Exception $e)
+    {
+        switch (get_class($e)) {
+            case Exception\AuthenticationException::class:
+            case Exception\ConnectionException::class:
+            case Exception\ConnectionTimeoutException::class:
+            case Exception\SSLConnectionException::class:
+                $class = 'MongoConnectionException';
+                break;
+
+            case Exception\BulkWriteException::class:
+            case Exception\WriteException::class:
+                $class = 'MongoCursorException';
+                break;
+
+            case Exception\ExecutionTimeoutException::class:
+                $class = 'MongoExecutionTimeoutException';
+                break;
+
+            default:
+                $class = 'MongoException';
+        }
+
+        if (strpos($e->getMessage(), 'No suitable servers found') !== false) {
+            return new \MongoConnectionException($e->getMessage(), $e->getCode(), $e);
+        }
+
+        return new $class($e->getMessage(), $e->getCode(), $e);
+    }
+
+    /**
+     * @throws \MongoException
+     */
+    public static function toLegacy(Exception\Exception $e)
+    {
+        throw self::convertException($e);
+    }
+}

+ 100 - 0
tests/Alcaeus/MongoDbAdapter/ExceptionConverterTest.php

@@ -0,0 +1,100 @@
+<?php
+
+namespace Alcaeus\MongoDbAdapter\Tests;
+
+use MongoDB\Driver\Exception;
+use Alcaeus\MongoDbAdapter\ExceptionConverter;
+
+class ExceptionConverterTest extends \PHPUnit_Framework_TestCase
+{
+    public function testThrowException()
+    {
+        $this->setExpectedException('MongoException');
+        ExceptionConverter::toLegacy(new Exception\InvalidArgumentException());
+    }
+
+    /**
+     * @dataProvider exceptionProvider
+     */
+    public function testConvertException($e, $expectedClass)
+    {
+        $exception = ExceptionConverter::convertException($e);
+        $this->assertInstanceOf($expectedClass, $exception);
+        $this->assertSame($e->getMessage(), $exception->getMessage());
+        $this->assertSame($e->getCode(), $exception->getCode());
+        $this->assertSame($e, $exception->getPrevious());
+    }
+
+    public function exceptionProvider()
+    {
+        return [
+            // Driver
+            [
+                new Exception\AuthenticationException('message', 1),
+                'MongoConnectionException',
+            ],
+            [
+                new Exception\BulkWriteException('message', 2),
+                'MongoCursorException',
+            ],
+            [
+                new Exception\ConnectionException('message', 2),
+                'MongoConnectionException',
+            ],
+            [
+                new Exception\ConnectionTimeoutException('message', 2),
+                'MongoConnectionException',
+            ],
+            [
+                new Exception\ExecutionTimeoutException('message', 2),
+                'MongoExecutionTimeoutException',
+            ],
+            [
+                new Exception\InvalidArgumentException('message', 2),
+                'MongoException',
+            ],
+            [
+                new Exception\LogicException('message', 2),
+                'MongoException',
+            ],
+            [
+                new Exception\RuntimeException('message', 2),
+                'MongoException',
+            ],
+            [
+                new Exception\SSLConnectionException('message', 2),
+                'MongoConnectionException',
+            ],
+            [
+                new Exception\UnexpectedValueException('message', 2),
+                'MongoException',
+            ],
+
+            // Library
+            [
+                new \MongoDB\Exception\BadMethodCallException('message', 2),
+                'MongoException',
+            ],
+            [
+                new \MongoDB\Exception\InvalidArgumentException('message', 2),
+                'MongoException',
+            ],
+            [
+                new \MongoDB\Exception\InvalidArgumentTypeException('message', 2, 'foo'),
+                'MongoException',
+            ],
+            [
+                new \MongoDB\Exception\UnexpectedTypeException('message', 2),
+                'MongoException',
+            ],
+            [
+                new \MongoDB\Exception\UnexpectedValueException('message', 2),
+                'MongoException',
+            ],
+            [
+                new \MongoDB\Exception\UnexpectedValueTypeException('message', 2, 'foo'),
+                'MongoException',
+            ],
+        ];
+    }
+}