MongoDB/Error.pm

filter: Statementstmt 81.0% 256 / 316 Branchbran 12.0% 7 / 58 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 77.4% 72 / 93 Podpod 0.0% 0 / 3 totaltotal 69.0% 335 / 485 CC 33 SCAR 27.5
CC
33
Cov
69%
_is_retryable
110.0
_is_unknown_commit_error
72.0
_check_is_retryable_message
12.0
CRAP 15.7
« MongoDB/_Credential.pm ↑ Summary MongoDB/Role/_InsertPreEncoder.pm »
1 # Copyright 2014 - present MongoDB, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 package MongoDB::Error;
19  
20 # ABSTRACT: MongoDB Driver Error classes
21  
22 # Portions adapted from Throwable.pm by Ricardo Signes
23  
24 70 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 70 our $VERSION = 'v2.2.2';
27  
28 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 70 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 70 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
31 ErrorStr
32 70 );
33 70 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
34 ArrayRef
35 Str
36 70 );
37 70 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 70 ▶use Sub::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 70 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 70 ▶use Exporter 5.57 qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 70 ▶use namespace::clean -except => ['import'];
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 70 my $ERROR_CODES;
44  
45 70 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
46 70 $ERROR_CODES = {
47 BAD_VALUE => 2,
48 HOST_UNREACHABLE => 6,
49 HOST_NOT_FOUND => 7,
50 UNKNOWN_ERROR => 8,
51 USER_NOT_FOUND => 11,
52 ILLEGAL_OPERATION => 20,
53 NAMESPACE_NOT_FOUND => 26,
54 INDEX_NOT_FOUND => 27,
55 CURSOR_NOT_FOUND => 43,
56 EXCEEDED_TIME_LIMIT => 50,
57 COMMAND_NOT_FOUND => 59,
58 WRITE_CONCERN_ERROR => 64,
59 NETWORK_TIMEOUT => 89,
60 SHUTDOWN_IN_PROGRESS => 91,
61 PRIMARY_STEPPED_DOWN => 189,
62 SOCKET_EXCEPTION => 9001,
63 NOT_MASTER => 10107,
64 DUPLICATE_KEY => 11000,
65 DUPLICATE_KEY_UPDATE => 11001, # legacy before 2.6
66 INTERRUPTED_AT_SHUTDOWN => 11600,
67 INTERRUPTED_DUE_TO_REPL_STATE_CHANGE => 11602,
68 DUPLICATE_KEY_CAPPED => 12582, # legacy before 2.6
69 UNRECOGNIZED_COMMAND => 13390, # mongos error before 2.4
70 NOT_MASTER_NO_SLAVE_OK => 13435,
71 NOT_MASTER_OR_SECONDARY => 13436,
72 CANT_OPEN_DB_IN_READ_LOCK => 15927,
73 };
74 }
75  
76 70 ▶use constant $ERROR_CODES;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 # Export error codes for use by end-users; this is unusual for Moo, but
79 # probably sufficiently helpful to justify it
80 70 our @EXPORT = keys %$ERROR_CODES;
81  
82 70 our %_HORRIBLE_HACK;
83  
84 70 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
85 523 ▶ q{""} => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 523 my $self = shift;
87 523 return sprintf( "%s: %s", ref($self), $self->message );
88 },
89 70 fallback => 1
90 70 );
91  
92 70 has message => (
93 is => 'ro',
94 isa => ErrorStr,
95 default => 'unspecified error',
96 );
97  
98 70 has 'previous_exception' => (
99 is => 'ro',
100 default => Sub::Quote::quote_sub(q<
101 if (defined $MongoDB::Error::_HORRIBLE_HACK{ERROR}) {
102 $MongoDB::Error::_HORRIBLE_HACK{ERROR}
103 } elsif (defined $@ and (ref $@ or length $@)) {
104 $@;
105 } else {
106 undef;
107 }
108 >),
109 );
110  
111 has error_labels => (
112 is => 'ro',
113 isa => ArrayRef[Str],
114 300 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 70 );
116  
117 0 ▶sub has_error_label {
Subroutine has_error_label: not called CC=2 SCAR=17.9
Pod undocumented
118 0 my ( $self, $expected ) = @_;
119  
120 0 ▶ return unless defined $self->error_labels;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->error_labels 0 0 0
121 0 return grep { $_ eq $expected } @{ $self->error_labels };
122 }
123  
124 0 ▶sub add_error_label {
Subroutine add_error_label: not called CC=2 SCAR=17.9
Pod undocumented
125 0 my ( $self, $label ) = @_;
126  
127 0 ▶ return if $self->has_error_label( $label );
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error_label($label) 0 0 0
128 0 push @{ $self->error_labels }, $label;
129 }
130  
131 300 ▶sub throw {
Subroutine throw: called CC=4 SCAR=15.9
Pod undocumented
132 300 my ($inv) = shift;
133  
134 300 ▶ if (Scalar::Util::blessed($inv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($inv)) 0 300 300
135 0 ▶ Carp::confess "throw called on MongoDB::Error object with arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
136 0 die $inv;
137 }
138  
139 300 local $_HORRIBLE_HACK{ERROR} = $@;
140  
141 300 ▶ my $throwable = @_ == 1 ? $inv->new( message => $_[0] ) : $inv->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 243 57 300
142  
143 300 die $throwable;
144 }
145  
146 # internal flag indicating if an operation should be retried when
147 # an error occurs.
148 0 ▶sub _is_resumable { 1 }
Subroutine _is_resumable: not called CC=1 SCAR=6.9
149  
150 # internal flag for if this error type specifically can be retried regardless
151 # of other state. See _is_retryable which contains the full retryable error
152 # logic.
153 0 ▶sub __is_retryable_error { 0 }
Subroutine __is_retryable_error: not called CC=1 SCAR=6.9
154  
155 70 my @retryable_codes = (
156 MongoDB::Error::HOST_NOT_FOUND(),
157 MongoDB::Error::HOST_UNREACHABLE(),
158 MongoDB::Error::NETWORK_TIMEOUT(),
159 MongoDB::Error::SHUTDOWN_IN_PROGRESS(),
160 MongoDB::Error::PRIMARY_STEPPED_DOWN(),
161 MongoDB::Error::SOCKET_EXCEPTION(),
162 MongoDB::Error::NOT_MASTER(),
163 MongoDB::Error::INTERRUPTED_AT_SHUTDOWN(),
164 MongoDB::Error::INTERRUPTED_DUE_TO_REPL_STATE_CHANGE(),
165 MongoDB::Error::NOT_MASTER_NO_SLAVE_OK(),
166 MongoDB::Error::NOT_MASTER_OR_SECONDARY(),
167 );
168  
169 0 ▶sub _check_is_retryable_code {
Subroutine _check_is_retryable_code: not called CC=2 SCAR=17.9
170 0 my $code = $_[-1];
171  
172 0 ▶ return 1 if grep { $code == $_ } @retryable_codes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$code == $_;} @retryable_codes 0 0 0
173 0 return 0;
174 }
175  
176 0 ▶sub _check_is_retryable_message {
Subroutine _check_is_retryable_message: not called CC=3 SCAR=24.8
177 0 my $message = $_[-1];
178  
179 0 ▶ return 0 unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
180 0 ▶ return 1 if $message =~ /(not master|node is recovering)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /(not master|node is recovering)/i 0 0 0
181 0 return 0;
182 }
183  
184 # indicates if this error can be retried under retryable writes
185 0 ▶sub _is_retryable {
Subroutine _is_retryable: not called CC=10 SCAR=47.0
186 0 my $self = shift;
187  
188 0 ▶ if ( $self->$_can( 'result' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$MongoDB::Error::_can("result")) 0 0 0
189 0 ▶ return 1 if _check_is_retryable_code( $self->result->last_code );
Branch: 1 branch
BranchTrueFalseTotal
if _check_is_retryable_code($self->result->last_code) 0 0 0
190 }
191  
192 0 ▶ if ( $self->$_can( 'code' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$MongoDB::Error::_can("code")) 0 0 0
193 0 ▶ return 1 if _check_is_retryable_code( $self->code );
Branch: 1 branch
BranchTrueFalseTotal
if _check_is_retryable_code($self->code) 0 0 0
194 }
195  
196 0 ▶ return 1 if _check_is_retryable_message( $self->message );
Branch: 1 branch
BranchTrueFalseTotal
if _check_is_retryable_message($self->message) 0 0 0
197  
198 0 ▶ if ( $self->$_isa( 'MongoDB::WriteConcernError' ) && $self->result->$_can( 'output' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$MongoDB::Error::_isa("MongoDB::WriteConcernError") and $self->result->$MongoDB::Error::_can("output")) 0 0 0
Condition0%0/3
$self->$MongoDB::Error::_isa("MongoDB::WriteConcernError") and $self->result->$MongoDB::Error::_can("output")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->$MongoDB::Error::_isa("MongoDB::WriteConcernError") and $self->result->$MongoDB::Error::_can("output")
$self->$MongoDB::Error::_isa("MongoDB::WriteConcernError") $self->result->$MongoDB::Error::_can("output")
Truth table0%0/3
A and B
A= $self->$MongoDB::Error::_isa("MongoDB::WriteConcernError")B= $self->result->$MongoDB::Error::_can("output")
ABresult
0X0
100
111
199 0 ▶ return 1 if _check_is_retryable_code( $self->result->output->{writeConcernError}{code} );
Branch: 1 branch
BranchTrueFalseTotal
if _check_is_retryable_code($self->result->output->{"writeConcernError"}{"code"}) 0 0 0
200 0 ▶ return 1 if _check_is_retryable_message( $self->result->output->{writeConcernError}{message} );
Branch: 1 branch
BranchTrueFalseTotal
if _check_is_retryable_message($self->result->output->{"writeConcernError"}{"message"}) 0 0 0
201 }
202  
203 # Defaults to 0 unless its a network exception
204 0 return $self->__is_retryable_error;
205 }
206  
207 70 my @unknown_commit_codes = (
208 MongoDB::Error::EXCEEDED_TIME_LIMIT(),
209 MongoDB::Error::WRITE_CONCERN_ERROR(),
210 );
211  
212 0 ▶sub _check_is_unknown_commit_code {
Subroutine _check_is_unknown_commit_code: not called CC=2 SCAR=17.9
213 0 my $code = $_[-1];
214  
215 0 ▶ return 1 if grep { $code == $_ } @unknown_commit_codes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$code == $_;} @unknown_commit_codes 0 0 0
216 0 return 0;
217 }
218  
219 0 ▶sub _is_unknown_commit_error {
Subroutine _is_unknown_commit_error: not called CC=8 SCAR=42.8
220 0 my $self = shift;
221  
222 0 ▶ return 1 if $self->isa("MongoDB::ConnectionError") || $self->isa("MongoDB::SelectionError");
Branch: 1 branch
BranchTrueFalseTotal
if $self->isa("MongoDB::ConnectionError") or $self->isa("MongoDB::SelectionError") 0 0 0
Condition0%0/3
$self->isa("MongoDB::ConnectionError") or $self->isa("MongoDB::SelectionError")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->isa("MongoDB::ConnectionError") or $self->isa("MongoDB::SelectionError")
$self->isa("MongoDB::ConnectionError") $self->isa("MongoDB::SelectionError")
Truth table0%0/3
A or B
A= $self->isa("MongoDB::ConnectionError")B= $self->isa("MongoDB::SelectionError")
ABresult
1X1
011
000
223  
224 0 ▶ return 1 if $self->_is_retryable;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_retryable 0 0 0
225  
226 0 ▶ if ( $self->$_can( 'result' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$MongoDB::Error::_can("result")) 0 0 0
227 0 ▶ return 1 if _check_is_unknown_commit_code( $self->result->last_code );
Branch: 1 branch
BranchTrueFalseTotal
if _check_is_unknown_commit_code($self->result->last_code) 0 0 0
228 }
229  
230 0 ▶ if ( $self->$_can( 'code' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$MongoDB::Error::_can("code")) 0 0 0
231 0 ▶ return 1 if _check_is_unknown_commit_code( $self->code );
Branch: 1 branch
BranchTrueFalseTotal
if _check_is_unknown_commit_code($self->code) 0 0 0
232 }
233  
234 0 return 0;
235 }
236  
237 0 ▶sub _is_transient_transaction_error {
Subroutine _is_transient_transaction_error: not called CC=3 SCAR=24.8
238 0 my $self = shift;
239 0 ▶ return 1 if $self->isa("MongoDB::ConnectionError") || $self->isa("MongoDB::SelectionError");
Branch: 1 branch
BranchTrueFalseTotal
if $self->isa("MongoDB::ConnectionError") or $self->isa("MongoDB::SelectionError") 0 0 0
Condition0%0/3
$self->isa("MongoDB::ConnectionError") or $self->isa("MongoDB::SelectionError")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->isa("MongoDB::ConnectionError") or $self->isa("MongoDB::SelectionError")
$self->isa("MongoDB::ConnectionError") $self->isa("MongoDB::SelectionError")
Truth table0%0/3
A or B
A= $self->isa("MongoDB::ConnectionError")B= $self->isa("MongoDB::SelectionError")
ABresult
1X1
011
000
240 0 return 0;
241 }
242  
243 # Look for error code ILLEGAL_OPERATION and starts with "Transaction numbers"
244 0 ▶sub _is_storage_engine_not_retryable {
Subroutine _is_storage_engine_not_retryable: not called CC=3 SCAR=24.8
245 0 my $self = shift;
246 0 ▶ if ( $self->$_can( 'code' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$MongoDB::Error::_can("code")) 0 0 0
247 0 ▶ return 0 if $self->code != MongoDB::Error::ILLEGAL_OPERATION;
Branch: 1 branch
BranchTrueFalseTotal
if $self->code != 20 0 0 0
248 }
249 0 return index($self->message, "Transaction numbers", 0) == 0;
250 }
251  
252 #--------------------------------------------------------------------------#
253 # Subclasses with attributes included inline below
254 #--------------------------------------------------------------------------#
255  
256 package MongoDB::DatabaseError;
257 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
258 70 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
259 Numish
260 70 );
261 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 70 extends("MongoDB::Error");
264  
265 70 has result => (
266 is => 'ro',
267 does => 'MongoDB::Role::_DatabaseErrorThrower',
268 required => 1,
269 );
270  
271 70 has code => (
272 is => 'ro',
273 isa => Numish,
274 builder => '_build_code',
275 );
276  
277 1 ▶sub _build_code { return MongoDB::Error::UNKNOWN_ERROR() }
Subroutine _build_code: called CC=1 SCAR=0.0
278  
279 0 ▶sub _is_resumable { 0 }
Subroutine _is_resumable: not called CC=1 SCAR=6.9
280  
281 package MongoDB::DocumentError;
282  
283 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
284 70 ▶use Types::Standard qw(Any);
Subroutine BEGIN: called CC=1 SCAR=0.0
285 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
286  
287 70 extends("MongoDB::Error");
288  
289 70 has document => (
290 is => 'ro',
291 isa => Any,
292 required => 1,
293 );
294  
295 package MongoDB::UsageError;
296  
297 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
298 70 ▶use Types::Standard qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
299 70 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
300  
301 70 extends("MongoDB::Error");
302  
303 70 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
304 35 ▶ q{""} => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
305 35 my $self = shift;
306 35 return sprintf( "%s: %s%s", ref($self), $self->message, $self->trace );
307 },
308 70 fallback => 1
309 70 );
310  
311 70 has trace => (
312 is => 'ro',
313 isa => Str,
314 );
315  
316 29 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
317 29 my $orig = shift;
318 29 my $class = shift;
319 29 my $args = $class->SUPER::BUILDARGS(@_);
320 # start stack trace above where throw() is called (or
321 # at the top of the stack), so it works like confess
322 29 my $i = 0;
323 29 ▶ while ( my @caller = caller($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i) 116 0 116
324 116 $i++;
325 116 ▶ last if $caller[0] eq "MongoDB::Error";
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] eq "MongoDB::Error" 29 87 116
326 }
327 29 ▶ local $Carp::CarpLevel = caller( $i + 1 ) ? $i + 1 : $i;
Branch: 1 branch
BranchTrueFalseTotal
caller $i + 1 ? : 29 0 29
328 29 $args->{trace} = Carp::longmess('');
329 29 return $args;
330 70 };
331  
332 # Connection errors
333 package MongoDB::ConnectionError;
334 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
335 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
336 70 extends 'MongoDB::Error';
337  
338 0 ▶sub _is_resumable { 1 }
Subroutine _is_resumable: not called CC=1 SCAR=6.9
339 0 ▶sub __is_retryable_error { 1 }
Subroutine __is_retryable_error: not called CC=1 SCAR=6.9
340  
341 package MongoDB::HandshakeError;
342 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
343 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
344 70 extends 'MongoDB::ConnectionError';
345  
346 package MongoDB::NetworkError;
347 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
348 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
349 70 extends 'MongoDB::ConnectionError';
350  
351 # Timeout errors
352 package MongoDB::TimeoutError;
353 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
354 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
355 70 extends 'MongoDB::Error';
356  
357 0 ▶sub __is_retryable_error { 1 }
Subroutine __is_retryable_error: not called CC=1 SCAR=6.9
358  
359 package MongoDB::ExecutionTimeout;
360 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
361 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
362 70 extends 'MongoDB::TimeoutError';
363  
364 package MongoDB::NetworkTimeout;
365 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
366 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
367 70 extends 'MongoDB::TimeoutError';
368  
369 # Database errors
370 package MongoDB::DuplicateKeyError;
371 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
372 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
373 70 extends 'MongoDB::DatabaseError';
374 0 ▶sub _build_code { return MongoDB::Error::DUPLICATE_KEY() }
Subroutine _build_code: not called CC=1 SCAR=6.9
375  
376 package MongoDB::NotMasterError;
377 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
378 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
379 70 extends 'MongoDB::DatabaseError';
380 0 ▶sub _build_code { return MongoDB::Error::NOT_MASTER() }
Subroutine _build_code: not called CC=1 SCAR=6.9
381 0 ▶sub _is_resumable { 1 }
Subroutine _is_resumable: not called CC=1 SCAR=6.9
382  
383 package MongoDB::WriteError;
384 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
385 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
386 70 extends 'MongoDB::DatabaseError';
387  
388 package MongoDB::WriteConcernError;
389 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
390 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
391 70 extends 'MongoDB::DatabaseError';
392 0 ▶sub _build_code { return MongoDB::Error::WRITE_CONCERN_ERROR() }
Subroutine _build_code: not called CC=1 SCAR=6.9
393  
394 # Other errors
395 package MongoDB::AuthError;
396 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
397 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
398 70 extends 'MongoDB::Error';
399  
400 package MongoDB::ConfigurationError;
401 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
402 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
403 70 extends 'MongoDB::Error';
404  
405 package MongoDB::CursorNotFoundError;
406 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
407 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
408 70 extends 'MongoDB::DatabaseError';
409 0 ▶sub _build_code { return MongoDB::Error::CURSOR_NOT_FOUND() }
Subroutine _build_code: not called CC=1 SCAR=6.9
410 0 ▶sub _is_resumable { 1 }
Subroutine _is_resumable: not called CC=1 SCAR=6.9
411  
412 package MongoDB::DecodingError;
413 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
414 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
415 70 extends 'MongoDB::Error';
416  
417 package MongoDB::GridFSError;
418 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
419 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
420 70 extends 'MongoDB::Error';
421  
422 package MongoDB::InternalError;
423 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
424 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
425 70 extends 'MongoDB::Error';
426  
427 package MongoDB::ProtocolError;
428 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
429 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
430 70 extends 'MongoDB::Error';
431  
432 package MongoDB::SelectionError;
433 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
434 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
435 70 extends 'MongoDB::Error';
436  
437 package MongoDB::InvalidOperationError;
438 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
439 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
440 70 extends 'MongoDB::Error';
441  
442 #--------------------------------------------------------------------------#
443 # Private error classes
444 #--------------------------------------------------------------------------#
445 package MongoDB::_CommandSizeError;
446 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
447 70 ▶use MongoDB::_Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
448 Intish
449 70 );
450 70 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
451  
452 70 extends("MongoDB::Error");
453  
454 70 has size => (
455 is => 'ro',
456 isa => Intish,
457 required => 1,
458 );
459  
460 70 1;
461  
462 =pod
463  
464 =encoding UTF-8
465  
466 =head1 NAME
467  
468 MongoDB::Error - MongoDB Driver Error classes
469  
470 =head1 VERSION
471  
472 version v2.2.2
473  
474 =head1 SYNOPSIS
475  
476 use MongoDB::Error;
477 MongoDB::Error->throw("a generic error");
478 MongoDB::DatabaseError->throw(
479 message => $string,
480 result => $hashref,
481 );
482  
483 =head1 DESCRIPTION
484  
485 This class defines a hierarchy of exception objects.
486  
487 =head1 USAGE
488  
489 Unless otherwise explicitly documented, all driver methods throw exceptions if
490 an error occurs.
491  
492 To catch and handle errors, the L<Try::Tiny> and L<Safe::Isa> modules
493 are recommended:
494  
495 use Try::Tiny;
496 use Safe::Isa; # provides $_isa
497  
498 try {
499 $coll->insert( $doc )
500 }
501 catch {
502 if ( $_->$_isa("MongoDB::DuplicateKeyError" ) ) {
503 ...
504 }
505 else {
506 ...
507 }
508 };
509  
510 To retry failures automatically, consider using L<Try::Tiny::Retry>.
511  
512 =head1 EXCEPTION HIERARCHY
513  
514 MongoDB::Error
515 |
516 |->MongoDB::AuthError
517 |
518 |->MongoDB::ConnectionError
519 | |
520 | |->MongoDB::HandshakeError
521 | |
522 | |->MongoDB::NetworkError
523 |
524 |->MongoDB::ConfigurationError
525 |
526 |->MongoDB::DatabaseError
527 | |
528 | |->MongoDB::CursorNotFoundError
529 | |
530 | |->MongoDB::DuplicateKeyError
531 | |
532 | |->MongoDB::NotMasterError
533 | |
534 | |->MongoDB::WriteError
535 | |
536 | |->MongoDB::WriteConcernError
537 |
538 |->MongoDB::DecodingError
539 |
540 |->MongoDB::DocumentError
541 |
542 |->MongoDB::GridFSError
543 |
544 |->MongoDB::InternalError
545 |
546 |->MongoDB::InvalidOperationError
547 |
548 |->MongoDB::ProtocolError
549 |
550 |->MongoDB::SelectionError
551 |
552 |->MongoDB::TimeoutError
553 | |
554 | |->MongoDB::ExecutionTimeout
555 | |
556 | |->MongoDB::NetworkTimeout
557 |
558 |->MongoDB::UsageError
559  
560 All classes inherit from C<MongoDB::Error>.
561  
562 All error classes have the attribute:
563  
564 =over 4
565  
566 =item *
567  
568 message — a text representation of the error
569  
570 =back
571  
572 =head2 MongoDB::AuthError
573  
574 This error indicates a problem with authentication, either in the underlying
575 mechanism or a problem authenticating with the server.
576  
577 =head2 MongoDB::ConnectionError
578  
579 Errors related to network connections.
580  
581 =head3 MongoDB::HandshakeError
582  
583 This error is thrown when a connection has been made, but SSL or authentication
584 handshakes fail.
585  
586 =head3 MongoDB::NetworkError
587  
588 This error is thrown when a socket error occurs, when the wrong number of bytes
589 are read, or other wire-related errors occur.
590  
591 =head2 MongoDB::ConfigurationError
592  
593 This error is thrown when there is a configuration error between the MongoDB
594 deployment and the configuration of the client, such as when trying to use
595 explicit sessions on a MongoDB < 3.6
596  
597 =head2 MongoDB::CursorNotFoundError
598  
599 This error indicates that a cursor timed out on a server.
600  
601 =head2 MongoDB::DatabaseError
602  
603 Errors related to database operations. Specifically, when an error of this type
604 occurs, the driver has received an error condition from the server.
605  
606 Attributes include:
607  
608 =over 4
609  
610 =item *
611  
612 result — response from a database command; this must implement the C<last_errmsg> method
613  
614 =item *
615  
616 code — numeric error code; see L</ERROR CODES>; if no code was provided by the database, the C<UNKNOWN_ERROR> code will be substituted instead
617  
618 =back
619  
620 =head3 MongoDB::DuplicateKeyError
621  
622 This error indicates that a write attempted to create a document with a
623 duplicate key in a collection with a unique index. The C<result> attribute is
624 a result object.
625  
626 =head3 MongoDB::NotMasterError
627  
628 This error indicates that a write or other state-modifying operation was
629 attempted on a server that was not a primary. The C<result> attribute is
630 a L<MongoDB::CommandResult> object.
631  
632 =head3 MongoDB::WriteError
633  
634 Errors indicating failure of a write command. The C<result> attribute is
635 a result object.
636  
637 =head3 MongoDB::WriteConcernError
638  
639 Errors indicating failure of a write concern. The C<result> attribute is a
640 result object.
641  
642 =head2 MongoDB::DecodingError
643  
644 This error indicates a problem during BSON decoding; it wraps
645 the error provided by the underlying BSON encoder. Note: Encoding errors
646 will be thrown as a L</MongoDB::DocumentError>.
647  
648 =head2 MongoDB::DocumentError
649  
650 This error indicates a problem with a document to be inserted or replaced into
651 the database, or used as an update document.
652  
653 Attributes include:
654  
655 =over 4
656  
657 =item *
658  
659 document — the document that caused the error
660  
661 =back
662  
663 =head2 MongoDB::GridFSError
664  
665 Errors related to GridFS operations, such a corrupted file.
666  
667 =head2 MongoDB::InternalError
668  
669 Errors that indicate problems in the driver itself, typically when something
670 unexpected is detected. These should be reported as potential bugs.
671  
672 =head2 MongoDB::ProtocolError
673  
674 Errors related to the MongoDB wire protocol, typically problems parsing a
675 database response packet.
676  
677 =head2 MongoDB::SelectionError
678  
679 When server selection fails for a given operation, this is thrown. For example,
680 attempting a write when no primary is available or reading with a specific mode
681 and tag set and no servers match.
682  
683 =head2 MongoDB::TimeoutError
684  
685 These errors indicate a user-specified timeout has been exceeded.
686  
687 =head3 MongoDB::ExecutionTimeout
688  
689 This error is thrown when a query or command fails because C<max_time_ms> has
690 been reached. The C<result> attribute is a L<MongoDB::CommandResult> object.
691  
692 =head3 MongoDB::NetworkTimeout
693  
694 This error is thrown when a network operation exceeds a timeout, typically
695 C<connect_timeout_ms> or C<socket_timeout_ms>.
696  
697 =head2 MongoDB::UsageError
698  
699 Indicates invalid arguments or configuration options. Not all usage errors
700 will throw this — only ones originating directly from the MongoDB::* library
701 files. Some type and usage errors will originate from the L<Type::Tiny>
702 library if the objects are used incorrectly.
703  
704 Also used to indicate usage errors for transaction commands.
705  
706 =head1 ERROR CODES
707  
708 The following error code constants are automatically exported by this module.
709  
710 BAD_VALUE => 2,
711 UNKNOWN_ERROR => 8,
712 NAMESPACE_NOT_FOUND => 26,
713 EXCEEDED_TIME_LIMIT => 50,
714 COMMAND_NOT_FOUND => 59,
715 WRITE_CONCERN_ERROR => 64,
716 NOT_MASTER => 10107,
717 DUPLICATE_KEY => 11000,
718 DUPLICATE_KEY_UPDATE => 11001, # legacy before 2.6
719 DUPLICATE_KEY_CAPPED => 12582, # legacy before 2.6
720 UNRECOGNIZED_COMMAND => 13390, # mongos error before 2.4
721 NOT_MASTER_NO_SLAVE_OK => 13435,
722 NOT_MASTER_OR_SECONDARY => 13436,
723 CANT_OPEN_DB_IN_READ_LOCK => 15927,
724  
725 This is a very, very small subset of error codes possible from the server,
726 but covers some of the more common ones seen by drivers.
727  
728 B<Note>:
729  
730 =over 4
731  
732 =item *
733  
734 Only C<MongoDB::DatabaseError> objects have a C<code> attribute.
735  
736 =item *
737  
738 The database uses multiple write concern error codes. The driver maps them all to WRITE_CONCERN_ERROR for consistency and convenience.
739  
740 =back
741  
742 =head1 ERROR LABELS
743  
744 From MongoDB 4.0 onwards, errors may contain an error labels field. This field
745 is populated for extra information from either the server or the driver,
746 depending on the error.
747  
748 Known error labels include (but are not limited to):
749  
750 =over 4
751  
752 =item *
753  
754 C<TransientTransactionError> - added when network errors are encountered inside a transaction.
755  
756 =item *
757  
758 C<UnknownTransactionCommitResult> - added when a transaction commit may not have been able to satisfy the provided write concern.
759  
760 =back
761  
762 =head1 AUTHORS
763  
764 =over 4
765  
766 =item *
767  
768 David Golden <david@mongodb.com>
769  
770 =item *
771  
772 Rassi <rassi@mongodb.com>
773  
774 =item *
775  
776 Mike Friedman <friedo@friedo.com>
777  
778 =item *
779  
780 Kristina Chodorow <k.chodorow@gmail.com>
781  
782 =item *
783  
784 Florian Ragwitz <rafl@debian.org>
785  
786 =back
787  
788 =head1 COPYRIGHT AND LICENSE
789  
790 This software is Copyright (c) 2020 by MongoDB, Inc.
791  
792 This is free software, licensed under:
793  
794 The Apache License, Version 2.0, January 2004
795  
796 =cut
797  
798 __END__
« MongoDB/_Credential.pm ↑ Summary MongoDB/Role/_InsertPreEncoder.pm »