1 | <?php |
||
23 | class ExceptionConverter |
||
24 | { |
||
25 | /** |
||
26 | * @param Exception\Exception $e |
||
27 | * @param string $fallbackClass |
||
28 | * |
||
29 | * @return \MongoException |
||
30 | */ |
||
31 | 13 | public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException') |
|
32 | { |
||
33 | 13 | $message = $e->getMessage(); |
|
34 | 13 | $code = $e->getCode(); |
|
35 | |||
36 | 13 | switch (get_class($e)) { |
|
37 | 13 | case Exception\AuthenticationException::class: |
|
38 | 13 | case Exception\ConnectionException::class: |
|
39 | 13 | case Exception\ConnectionTimeoutException::class: |
|
40 | 13 | case Exception\SSLConnectionException::class: |
|
41 | 4 | $class = 'MongoConnectionException'; |
|
42 | 4 | break; |
|
43 | |||
44 | 9 | case Exception\BulkWriteException::class: |
|
45 | 9 | case Exception\WriteException::class: |
|
46 | 1 | $writeResult = $e->getWriteResult(); |
|
47 | |||
48 | 1 | 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 | 1 | case 11000: |
|
58 | 1 | case 11001: |
|
59 | 1 | case 12582: |
|
60 | $class = 'MongoDuplicateKeyException'; |
||
61 | break; |
||
62 | 1 | default: |
|
63 | 1 | $class = 'MongoCursorException'; |
|
64 | 1 | } |
|
65 | 1 | break; |
|
66 | |||
67 | 8 | case Exception\ExecutionTimeoutException::class: |
|
68 | 1 | $class = 'MongoExecutionTimeoutException'; |
|
69 | 1 | break; |
|
70 | |||
71 | 7 | default: |
|
72 | 7 | $class = $fallbackClass; |
|
73 | 13 | } |
|
74 | |||
75 | 13 | if (strpos($message, 'No suitable servers found') !== false) { |
|
76 | return new \MongoConnectionException($message, $code, $e); |
||
77 | } |
||
78 | |||
79 | 13 | if ($message === "cannot use 'w' > 1 when a host is not replicated") { |
|
80 | return new \MongoWriteConcernException($message, $code, $e); |
||
81 | } |
||
82 | |||
83 | 13 | 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) |
||
100 | } |
||
101 |