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 60     60   93970 use strict;
  60         131  
  60         1800  
16 60     60   307 use warnings;
  60         119  
  60         4210  
17             package MongoDB::Role::_DatabaseErrorThrower;
18              
19             # MongoDB interface for providing the last database error
20              
21 60     60   2198 use version;
  60         2245  
  60         2156  
22             our $VERSION = 'v2.2.2';
23              
24 60     60   4377 use Moo::Role;
  60         117  
  60         2530  
25              
26 60     60   22314 use MongoDB::Error;
  60         161  
  60         5996  
27              
28 60     60   364 use namespace::clean;
  60         107  
  60         339  
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;