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