1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 */
15
16namespace Alcaeus\MongoDbAdapter;
17
18use MongoDB\Driver\Exception;
19
20/**
21 * @internal
22 */
23class ExceptionConverter
24{
25    /**
26     * @param Exception\Exception $e
27     * @param string $fallbackClass
28     *
29     * @return \MongoException
30     */
31    public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
32    {
33        $message = $e->getMessage();
34        $code = $e->getCode();
35
36        switch (get_class($e)) {
37            case Exception\AuthenticationException::class:
38            case Exception\ConnectionException::class:
39            case Exception\ConnectionTimeoutException::class:
40            case Exception\SSLConnectionException::class:
41                $class = 'MongoConnectionException';
42                break;
43
44            case Exception\BulkWriteException::class:
45            case Exception\WriteException::class:
46                $writeResult = $e->getWriteResult();
47
48                if ($writeResult) {
49                    $writeError = $writeResult->getWriteErrors()[0];
50
51                    $message = $writeError->getMessage();
52                    $code = $writeError->getCode();
53                }
54
55                switch ($code) {
56                    // see https://github.com/mongodb/mongo-php-driver-legacy/blob/ad3ed45739e9702ae48e53ddfadc482d9c4c7e1c/cursor_shared.c#L540
57                    case 11000:
58                    case 11001:
59                    case 12582:
60                        $class = 'MongoDuplicateKeyException';
61                        break;
62                    default:
63                        $class = 'MongoCursorException';
64                }
65                break;
66
67            case Exception\ExecutionTimeoutException::class:
68                $class = 'MongoExecutionTimeoutException';
69                break;
70
71            default:
72                $class = $fallbackClass;
73        }
74
75        if (strpos($message, 'No suitable servers found') !== false) {
76            return new \MongoConnectionException($message, $code, $e);
77        }
78
79        if ($message === "cannot use 'w' > 1 when a host is not replicated") {
80            return new \MongoWriteConcernException($message, $code, $e);
81        }
82
83        return new $class($message, $code, $e);
84    }
85
86    /**
87     * Converts an exception to
88     *
89     * @param Exception\Exception $e
90     * @return array
91     */
92    public static function toResultArray(Exception\Exception $e)
93    {
94        return [
95            'ok' => 0.0,
96            'errmsg' => $e->getMessage(),
97            'code' => $e->getCode(),
98        ];
99    }
100}
101