DbalException.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\DB\Exceptions;
  8. use Doctrine\DBAL\ConnectionException;
  9. use Doctrine\DBAL\Exception\ConstraintViolationException;
  10. use Doctrine\DBAL\Exception\DatabaseObjectExistsException;
  11. use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
  12. use Doctrine\DBAL\Exception\DeadlockException;
  13. use Doctrine\DBAL\Exception\DriverException;
  14. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  15. use Doctrine\DBAL\Exception\InvalidArgumentException;
  16. use Doctrine\DBAL\Exception\InvalidFieldNameException;
  17. use Doctrine\DBAL\Exception\LockWaitTimeoutException;
  18. use Doctrine\DBAL\Exception\NonUniqueFieldNameException;
  19. use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
  20. use Doctrine\DBAL\Exception\RetryableException;
  21. use Doctrine\DBAL\Exception\ServerException;
  22. use Doctrine\DBAL\Exception\SyntaxErrorException;
  23. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  24. use OCP\DB\Exception;
  25. /**
  26. * Wrapper around the raw dbal exception, so we can pass it to apps that catch
  27. * our OCP db exception
  28. *
  29. * @psalm-immutable
  30. */
  31. class DbalException extends Exception {
  32. /** @var \Doctrine\DBAL\Exception */
  33. private $original;
  34. /**
  35. * @param \Doctrine\DBAL\Exception $original
  36. * @param int $code
  37. * @param string $message
  38. */
  39. private function __construct(\Doctrine\DBAL\Exception $original, int $code, string $message) {
  40. parent::__construct(
  41. $message,
  42. $code,
  43. $original
  44. );
  45. $this->original = $original;
  46. }
  47. public static function wrap(\Doctrine\DBAL\Exception $original, string $message = ''): self {
  48. return new self(
  49. $original,
  50. is_int($original->getCode()) ? $original->getCode() : 0,
  51. empty($message) ? $original->getMessage() : $message
  52. );
  53. }
  54. public function isRetryable(): bool {
  55. return $this->original instanceof RetryableException;
  56. }
  57. public function getReason(): ?int {
  58. /**
  59. * Constraint errors
  60. */
  61. if ($this->original instanceof ForeignKeyConstraintViolationException) {
  62. return parent::REASON_FOREIGN_KEY_VIOLATION;
  63. }
  64. if ($this->original instanceof NotNullConstraintViolationException) {
  65. return parent::REASON_NOT_NULL_CONSTRAINT_VIOLATION;
  66. }
  67. if ($this->original instanceof UniqueConstraintViolationException) {
  68. return parent::REASON_UNIQUE_CONSTRAINT_VIOLATION;
  69. }
  70. // The base exception comes last
  71. if ($this->original instanceof ConstraintViolationException) {
  72. return parent::REASON_CONSTRAINT_VIOLATION;
  73. }
  74. /**
  75. * Other server errors
  76. */
  77. if ($this->original instanceof LockWaitTimeoutException) {
  78. return parent::REASON_LOCK_WAIT_TIMEOUT;
  79. }
  80. if ($this->original instanceof DatabaseObjectExistsException) {
  81. return parent::REASON_DATABASE_OBJECT_EXISTS;
  82. }
  83. if ($this->original instanceof DatabaseObjectNotFoundException) {
  84. return parent::REASON_DATABASE_OBJECT_NOT_FOUND;
  85. }
  86. if ($this->original instanceof DeadlockException) {
  87. return parent::REASON_DEADLOCK;
  88. }
  89. if ($this->original instanceof InvalidFieldNameException) {
  90. return parent::REASON_INVALID_FIELD_NAME;
  91. }
  92. if ($this->original instanceof NonUniqueFieldNameException) {
  93. return parent::REASON_NON_UNIQUE_FIELD_NAME;
  94. }
  95. if ($this->original instanceof SyntaxErrorException) {
  96. return parent::REASON_SYNTAX_ERROR;
  97. }
  98. // The base server exception class comes last
  99. if ($this->original instanceof ServerException) {
  100. return parent::REASON_SERVER;
  101. }
  102. /**
  103. * Generic errors
  104. */
  105. if ($this->original instanceof ConnectionException) {
  106. return parent::REASON_CONNECTION_LOST;
  107. }
  108. if ($this->original instanceof InvalidArgumentException) {
  109. return parent::REASON_INVALID_ARGUMENT;
  110. }
  111. if ($this->original instanceof DriverException) {
  112. return parent::REASON_DRIVER;
  113. }
  114. return null;
  115. }
  116. }