File Coverage

blib/lib/MongoDB/Role/_WriteResult.pm
Criterion Covered Total %
statement 24 62 38.7
branch 0 20 0.0
condition 0 11 0.0
subroutine 8 18 44.4
pod 0 10 0.0
total 32 121 26.4


line stmt bran cond sub pod time code
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 60     60   97758 use strict;
  60         156  
  60         1943  
16 60     60   339 use warnings;
  60         133  
  60         2255  
17             package MongoDB::Role::_WriteResult;
18              
19             # MongoDB interface for common write result attributes and methods
20              
21 60     60   334 use version;
  60         141  
  60         330  
22             our $VERSION = 'v2.2.1';
23              
24 60     60   4441 use Moo::Role;
  60         138  
  60         442  
25              
26 60     60   20217 use MongoDB::Error;
  60         150  
  60         5885  
27 60     60   401 use MongoDB::_Constants;
  60         143  
  60         6020  
28 60         433 use MongoDB::_Types qw(
29             ArrayOfHashRef
30 60     60   375 );
  60         139  
31              
32 60     60   58363 use namespace::clean;
  60         147  
  60         359  
33              
34             has [qw/write_errors write_concern_errors/] => (
35             is => 'ro',
36             required => 1,
37             isa => ArrayOfHashRef,
38             );
39              
40             with 'MongoDB::Role::_DatabaseErrorThrower';
41              
42 0     0 0   sub acknowledged { 1 }; # override to 0 for MongoDB::UnacknowledgedResult
43              
44             # inline assert_no_write_error and assert_no_write_concern rather
45             # than having to make to additional method calls
46             sub assert {
47 0     0 0   my ($self) = @_;
48              
49             $self->_throw_database_error("MongoDB::WriteError")
50 0 0         if scalar @{ $self->write_errors };
  0            
51              
52             MongoDB::WriteConcernError->throw(
53             message => $self->last_errmsg,
54             result => $self,
55             code => WRITE_CONCERN_ERROR,
56 0 0         ) if scalar @{ $self->write_concern_errors };
  0            
57              
58 0           return $self;
59             }
60              
61             sub assert_no_write_error {
62 0     0 0   my ($self) = @_;
63              
64             $self->_throw_database_error("MongoDB::WriteError")
65 0 0         if scalar @{ $self->write_errors };
  0            
66              
67 0           return $self;
68             }
69              
70             sub assert_no_write_concern_error {
71 0     0 0   my ($self) = @_;
72              
73             MongoDB::WriteConcernError->throw(
74             message => $self->last_errmsg,
75             result => $self,
76             code => WRITE_CONCERN_ERROR,
77 0 0         ) if scalar @{ $self->write_concern_errors };
  0            
78              
79 0           return $self;
80             }
81              
82             sub count_write_errors {
83 0     0 0   my ($self) = @_;
84 0           return scalar @{ $self->write_errors };
  0            
85             }
86              
87             sub count_write_concern_errors {
88 0     0 0   my ($self) = @_;
89 0           return scalar @{ $self->write_concern_errors };
  0            
90             }
91              
92             sub last_errmsg {
93 0     0 0   my ($self) = @_;
94 0 0         if ( $self->count_write_errors ) {
    0          
95 0           return $self->write_errors->[-1]{errmsg};
96             }
97             elsif ( $self->count_write_concern_errors ) {
98 0           return $self->write_concern_errors->[-1]{errmsg};
99             }
100             else {
101 0           return "";
102             }
103             }
104              
105             sub last_code {
106 0     0 0   my ($self) = @_;
107 0 0         if ( $self->count_write_errors ) {
    0          
108 0   0       return $self->write_errors->[-1]{code} || UNKNOWN_ERROR;
109             }
110             elsif ( $self->count_write_concern_errors ) {
111 0   0       return $self->write_concern_errors->[-1]{code} || UNKNOWN_ERROR;
112             }
113             else {
114 0           return 0;
115             }
116             }
117              
118             sub last_wtimeout {
119 0     0 0   my ($self) = @_;
120             # if we have actual write errors, we don't want to report a
121             # write concern error
122 0   0       return !!( $self->count_write_concern_errors && !$self->count_write_errors );
123             }
124              
125             sub last_error_labels {
126 0     0 0   my ( $self ) = @_;
127 0 0         if ( $self->count_write_errors ) {
    0          
128 0   0       return $self->write_errors->[-1]{errorLabels} || [];
129             }
130             elsif ( $self->count_write_concern_errors ) {
131 0   0       return $self->write_errors->[-1]{errorLabels} || [];
132             }
133 0           return [];
134             }
135              
136             1;