File Coverage

blib/lib/MongoDB/Role/_DatabaseErrorThrower.pm
Criterion Covered Total %
statement 18 31 58.0
branch 0 10 0.0
condition 0 7 0.0
subroutine 6 7 85.7
pod n/a
total 24 55 43.6


line stmt bran cond sub pod time code
1             # Copyright 2016 - 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 59     59   101726 use strict;
  59         148  
  59         1866  
16 59     59   355 use warnings;
  59         127  
  59         2420  
17             package MongoDB::Role::_DatabaseErrorThrower;
18              
19             # MongoDB interface for providing the last database error
20              
21 59     59   322 use version;
  59         116  
  59         365  
22             our $VERSION = 'v2.2.0';
23              
24 59     59   4616 use Moo::Role;
  59         144  
  59         2496  
25              
26 59     59   29096 use MongoDB::Error;
  59         178  
  59         5989  
27              
28 59     59   379 use namespace::clean;
  59         2119  
  59         2516  
29              
30             requires qw/last_errmsg last_code last_wtimeout last_error_labels/;
31              
32             my $ANY_DUP_KEY = [ DUPLICATE_KEY, DUPLICATE_KEY_UPDATE, DUPLICATE_KEY_CAPPED ];
33             my $ANY_NOT_MASTER = [ NOT_MASTER, NOT_MASTER_NO_SLAVE_OK, NOT_MASTER_OR_SECONDARY ];
34              
35             # analyze last_errmsg and last_code and throw an appropriate
36             # error message.
37             sub _throw_database_error {
38 0     0     my ( $self, $error_class ) = @_;
39 0   0       $error_class ||= "MongoDB::DatabaseError";
40              
41 0           my $err = $self->last_errmsg;
42 0           my $code = $self->last_code;
43 0           my $error_labels = $self->last_error_labels;
44              
45 0 0 0       if ( grep { $code == $_ } @$ANY_NOT_MASTER || $err =~ /^(?:not master|node is recovering)/ ) {
  0 0          
    0          
    0          
46 0           $error_class = "MongoDB::NotMasterError";
47             }
48 0           elsif ( grep { $code == $_ } @$ANY_DUP_KEY ) {
49 0           $error_class = "MongoDB::DuplicateKeyError";
50             }
51             elsif ( $code == CURSOR_NOT_FOUND ) {
52 0           $error_class = "MongoDB::CursorNotFoundError";
53             }
54             elsif ( $self->last_wtimeout ) {
55 0           $error_class = "MongoDB::WriteConcernError";
56             }
57              
58             $error_class->throw(
59 0 0 0       result => $self,
60             code => $code || UNKNOWN_ERROR,
61             error_labels => $error_labels,
62             ( length($err) ? ( message => $err ) : () ),
63             );
64              
65             }
66              
67             1;