-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathException.php
More file actions
259 lines (230 loc) · 6.02 KB
/
Exception.php
File metadata and controls
259 lines (230 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
namespace Utopia\Mongo;
/**
* Base MongoDB exception class with enhanced error categorization.
*
* Provides better error handling and categorization for MongoDB 8+ operations.
*/
class Exception extends \Exception
{
protected array $errorLabels = [];
protected ?array $writeErrors = null;
protected ?array $writeConcernErrors = null;
protected ?string $operationType = null;
/**
* @param string $message Error message
* @param int $code Error code
* @param \Throwable|null $previous Previous exception
* @param array $errorLabels MongoDB error labels
* @param array|null $writeErrors Write errors if applicable
* @param array|null $writeConcernErrors Write concern errors if applicable
* @param string|null $operationType Operation type that caused the error
*/
public function __construct(
string $message = '',
int $code = 0,
?\Throwable $previous = null,
array $errorLabels = [],
?array $writeErrors = null,
?array $writeConcernErrors = null,
?string $operationType = null
) {
parent::__construct($message, $code, $previous);
$this->errorLabels = $errorLabels;
$this->writeErrors = $writeErrors;
$this->writeConcernErrors = $writeConcernErrors;
$this->operationType = $operationType;
}
/**
* Check if this is a transient error that can be retried.
*
* @return bool
*/
public function isTransientError(): bool
{
return in_array('TransientTransactionError', $this->errorLabels) ||
in_array('UnknownTransactionCommitResult', $this->errorLabels) ||
$this->isNetworkError() ||
$this->isRetryableWrite();
}
/**
* Check if this is a network-related error.
*
* @return bool
*/
public function isNetworkError(): bool
{
$networkErrorCodes = [
11600, 11601, 11602, // Socket errors
89, // NetworkTimeout
9001, // SocketException
6, // HostUnreachable
7, // HostNotFound
];
return in_array($this->code, $networkErrorCodes);
}
/**
* Check if this is a retryable write error.
*
* @return bool
*/
public function isRetryableWrite(): bool
{
return in_array('RetryableWriteError', $this->errorLabels);
}
/**
* Check if this is a duplicate key error.
*
* @return bool
*/
public function isDuplicateKeyError(): bool
{
return $this->code === 11000 || $this->code === 11001;
}
/**
* Check if this is a write concern error.
*
* @return bool
*/
public function isWriteConcernError(): bool
{
return !empty($this->writeConcernErrors);
}
/**
* Check if this is a timeout error.
*
* @return bool
*/
public function isTimeoutError(): bool
{
$timeoutCodes = [
50, // MaxTimeMSExpired
89, // NetworkTimeout
11601, // SocketTimeout
];
return in_array($this->code, $timeoutCodes);
}
/**
* Get error labels.
*
* @return array
*/
public function getErrorLabels(): array
{
return $this->errorLabels;
}
/**
* Get write errors.
*
* @return array|null
*/
public function getWriteErrors(): ?array
{
return $this->writeErrors;
}
/**
* Get write concern errors.
*
* @return array|null
*/
public function getWriteConcernErrors(): ?array
{
return $this->writeConcernErrors;
}
/**
* Get the operation type that caused this error.
*
* @return string|null
*/
public function getOperationType(): ?string
{
return $this->operationType;
}
/**
* Get a human-readable error category.
*
* @return string
*/
public function getErrorCategory(): string
{
if ($this->isNetworkError()) {
return 'Network Error';
}
if ($this->isTimeoutError()) {
return 'Timeout Error';
}
if ($this->isDuplicateKeyError()) {
return 'Duplicate Key Error';
}
if ($this->isWriteConcernError()) {
return 'Write Concern Error';
}
if ($this->isTransientError()) {
return 'Transient Error';
}
return 'MongoDB Error';
}
/**
* Create exception from MongoDB error response.
*
* @param \stdClass $errorResponse MongoDB error response
* @param string|null $operationType Operation type
* @return self
*/
public static function fromResponse(\stdClass $errorResponse, ?string $operationType = null): self
{
$message = $errorResponse->errmsg ?? 'Unknown MongoDB error';
$code = $errorResponse->code ?? 0;
$errorLabels = $errorResponse->errorLabels ?? [];
$writeErrors = $errorResponse->writeErrors ?? null;
$writeConcernErrors = $errorResponse->writeConcernErrors ?? null;
return new self(
$message,
$code,
null,
$errorLabels,
$writeErrors,
$writeConcernErrors,
$operationType
);
}
}
/**
* Connection-related exception.
*/
class ConnectionException extends Exception
{
}
/**
* Authentication-related exception.
*/
class AuthenticationException extends Exception
{
}
/**
* Transaction-related exception.
*/
class TransactionException extends Exception
{
}
/**
* Bulk write operation exception.
*/
class BulkWriteException extends Exception
{
private array $result;
public function __construct(string $message, array $result, int $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->result = $result;
}
/**
* Get the bulk write result.
*
* @return array
*/
public function getResult(): array
{
return $this->result;
}
}