ExceptionConverter.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. namespace Alcaeus\MongoDbAdapter;
  16. use MongoDB\Driver\Exception;
  17. use MongoDB\Driver\WriteError;
  18. use MongoDB\Driver\WriteResult;
  19. /**
  20. * @internal
  21. */
  22. class ExceptionConverter
  23. {
  24. /**
  25. * @param Exception\Exception $e
  26. * @param string $fallbackClass
  27. *
  28. * @return \MongoException
  29. */
  30. public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
  31. {
  32. $message = $e->getMessage();
  33. $code = $e->getCode();
  34. switch (get_class($e)) {
  35. case Exception\AuthenticationException::class:
  36. case Exception\ConnectionException::class:
  37. case Exception\ConnectionTimeoutException::class:
  38. case Exception\SSLConnectionException::class:
  39. $class = 'MongoConnectionException';
  40. break;
  41. case Exception\BulkWriteException::class:
  42. case Exception\WriteException::class:
  43. $writeResult = $e->getWriteResult();
  44. // attempt to retrieve write error
  45. if ($writeResult instanceof WriteResult && is_array($writeResult->getWriteErrors()) && $writeResult->getWriteErrors() !== []) {
  46. $writeError = $writeResult->getWriteErrors()[0];
  47. if ($writeError instanceof WriteError) {
  48. $message = $writeError->getMessage();
  49. $code = $writeError->getCode();
  50. }
  51. }
  52. switch ($code) {
  53. // see https://github.com/mongodb/mongo-php-driver-legacy/blob/ad3ed45739e9702ae48e53ddfadc482d9c4c7e1c/cursor_shared.c#L540
  54. case 11000:
  55. case 11001:
  56. case 12582:
  57. $class = 'MongoDuplicateKeyException';
  58. break;
  59. default:
  60. $class = 'MongoCursorException';
  61. }
  62. break;
  63. case Exception\ExecutionTimeoutException::class:
  64. $class = 'MongoExecutionTimeoutException';
  65. break;
  66. default:
  67. $class = $fallbackClass;
  68. }
  69. if (strpos($message, 'No suitable servers found') !== false) {
  70. return new \MongoConnectionException($message, $code, $e);
  71. }
  72. if ($message === "cannot use 'w' > 1 when a host is not replicated") {
  73. return new \MongoWriteConcernException($message, $code, $e);
  74. }
  75. return new $class($message, $code, $e);
  76. }
  77. /**
  78. * Converts an exception to
  79. *
  80. * @param Exception\Exception $e
  81. * @return array
  82. */
  83. public static function toResultArray(Exception\Exception $e)
  84. {
  85. return [
  86. 'ok' => 0.0,
  87. 'errmsg' => $e->getMessage(),
  88. 'code' => $e->getCode(),
  89. ];
  90. }
  91. }