DbalException.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\DB\Exceptions;
  26. use Doctrine\DBAL\ConnectionException;
  27. use Doctrine\DBAL\Exception\ConstraintViolationException;
  28. use Doctrine\DBAL\Exception\DatabaseObjectExistsException;
  29. use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
  30. use Doctrine\DBAL\Exception\DeadlockException;
  31. use Doctrine\DBAL\Exception\DriverException;
  32. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  33. use Doctrine\DBAL\Exception\InvalidArgumentException;
  34. use Doctrine\DBAL\Exception\InvalidFieldNameException;
  35. use Doctrine\DBAL\Exception\NonUniqueFieldNameException;
  36. use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
  37. use Doctrine\DBAL\Exception\RetryableException;
  38. use Doctrine\DBAL\Exception\ServerException;
  39. use Doctrine\DBAL\Exception\SyntaxErrorException;
  40. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  41. use OCP\DB\Exception;
  42. /**
  43. * Wrapper around the raw dbal exception, so we can pass it to apps that catch
  44. * our OCP db exception
  45. *
  46. * @psalm-immutable
  47. */
  48. class DbalException extends Exception {
  49. /** @var \Doctrine\DBAL\Exception */
  50. private $original;
  51. /**
  52. * @param \Doctrine\DBAL\Exception $original
  53. * @param int $code
  54. * @param string $message
  55. */
  56. private function __construct(\Doctrine\DBAL\Exception $original, int $code, string $message) {
  57. parent::__construct(
  58. $message,
  59. $code,
  60. $original
  61. );
  62. $this->original = $original;
  63. }
  64. public static function wrap(\Doctrine\DBAL\Exception $original, string $message = ''): self {
  65. return new self(
  66. $original,
  67. is_int($original->getCode()) ? $original->getCode() : 0,
  68. empty($message) ? $original->getMessage() : $message
  69. );
  70. }
  71. public function isRetryable(): bool {
  72. return $this->original instanceof RetryableException;
  73. }
  74. public function getReason(): ?int {
  75. /**
  76. * Constraint errors
  77. */
  78. if ($this->original instanceof ForeignKeyConstraintViolationException) {
  79. return parent::REASON_FOREIGN_KEY_VIOLATION;
  80. }
  81. if ($this->original instanceof NotNullConstraintViolationException) {
  82. return parent::REASON_NOT_NULL_CONSTRAINT_VIOLATION;
  83. }
  84. if ($this->original instanceof UniqueConstraintViolationException) {
  85. return parent::REASON_UNIQUE_CONSTRAINT_VIOLATION;
  86. }
  87. // The base exception comes last
  88. if ($this->original instanceof ConstraintViolationException) {
  89. return parent::REASON_CONSTRAINT_VIOLATION;
  90. }
  91. /**
  92. * Other server errors
  93. */
  94. if ($this->original instanceof DatabaseObjectExistsException) {
  95. return parent::REASON_DATABASE_OBJECT_EXISTS;
  96. }
  97. if ($this->original instanceof DatabaseObjectNotFoundException) {
  98. return parent::REASON_DATABASE_OBJECT_NOT_FOUND;
  99. }
  100. if ($this->original instanceof DeadlockException) {
  101. return parent::REASON_DEADLOCK;
  102. }
  103. if ($this->original instanceof InvalidFieldNameException) {
  104. return parent::REASON_INVALID_FIELD_NAME;
  105. }
  106. if ($this->original instanceof NonUniqueFieldNameException) {
  107. return parent::REASON_NON_UNIQUE_FIELD_NAME;
  108. }
  109. if ($this->original instanceof SyntaxErrorException) {
  110. return parent::REASON_SYNTAX_ERROR;
  111. }
  112. // The base server exception class comes last
  113. if ($this->original instanceof ServerException) {
  114. return parent::REASON_SERVER;
  115. }
  116. /**
  117. * Generic errors
  118. */
  119. if ($this->original instanceof ConnectionException) {
  120. return parent::REASON_CONNECTION_LOST;
  121. }
  122. if ($this->original instanceof InvalidArgumentException) {
  123. return parent::REASON_INVALID_ARGUMENT;
  124. }
  125. if ($this->original instanceof DriverException) {
  126. return parent::REASON_DRIVER;
  127. }
  128. return null;
  129. }
  130. }