DbalException.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\ServerException;
  38. use Doctrine\DBAL\Exception\SyntaxErrorException;
  39. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  40. use OCP\DB\Exception;
  41. /**
  42. * Wrapper around the raw dbal exception, so we can pass it to apps that catch
  43. * our OCP db exception
  44. *
  45. * @psalm-immutable
  46. */
  47. class DbalException extends Exception {
  48. /** @var \Doctrine\DBAL\Exception */
  49. private $original;
  50. /**
  51. * @param \Doctrine\DBAL\Exception $original
  52. * @param int $code
  53. * @param string $message
  54. */
  55. private function __construct(\Doctrine\DBAL\Exception $original, int $code, string $message) {
  56. parent::__construct(
  57. $message,
  58. $code,
  59. $original
  60. );
  61. $this->original = $original;
  62. }
  63. public static function wrap(\Doctrine\DBAL\Exception $original, string $message = ''): self {
  64. return new self(
  65. $original,
  66. is_int($original->getCode()) ? $original->getCode() : 0,
  67. empty($message) ? $original->getMessage() : $message
  68. );
  69. }
  70. public function getReason(): ?int {
  71. /**
  72. * Constraint errors
  73. */
  74. if ($this->original instanceof ForeignKeyConstraintViolationException) {
  75. return parent::REASON_FOREIGN_KEY_VIOLATION;
  76. }
  77. if ($this->original instanceof NotNullConstraintViolationException) {
  78. return parent::REASON_NOT_NULL_CONSTRAINT_VIOLATION;
  79. }
  80. if ($this->original instanceof UniqueConstraintViolationException) {
  81. return parent::REASON_UNIQUE_CONSTRAINT_VIOLATION;
  82. }
  83. // The base exception comes last
  84. if ($this->original instanceof ConstraintViolationException) {
  85. return parent::REASON_CONSTRAINT_VIOLATION;
  86. }
  87. /**
  88. * Other server errors
  89. */
  90. if ($this->original instanceof DatabaseObjectExistsException) {
  91. return parent::REASON_DATABASE_OBJECT_EXISTS;
  92. }
  93. if ($this->original instanceof DatabaseObjectNotFoundException) {
  94. return parent::REASON_DATABASE_OBJECT_NOT_FOUND;
  95. }
  96. if ($this->original instanceof DeadlockException) {
  97. return parent::REASON_DEADLOCK;
  98. }
  99. if ($this->original instanceof InvalidFieldNameException) {
  100. return parent::REASON_INVALID_FIELD_NAME;
  101. }
  102. if ($this->original instanceof NonUniqueFieldNameException) {
  103. return parent::REASON_NON_UNIQUE_FIELD_NAME;
  104. }
  105. if ($this->original instanceof SyntaxErrorException) {
  106. return parent::REASON_SYNTAX_ERROR;
  107. }
  108. // The base server exception class comes last
  109. if ($this->original instanceof ServerException) {
  110. return parent::REASON_SERVER;
  111. }
  112. /**
  113. * Generic errors
  114. */
  115. if ($this->original instanceof ConnectionException) {
  116. return parent::REASON_CONNECTION_LOST;
  117. }
  118. if ($this->original instanceof InvalidArgumentException) {
  119. return parent::REASON_INVALID_ARGUMENT;
  120. }
  121. if ($this->original instanceof DriverException) {
  122. return parent::REASON_DRIVER;
  123. }
  124. return null;
  125. }
  126. }