Client/Error.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 90.0% 27 / 30 Conditioncond 78.7% 26 / 33 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 89.6% 156 / 174 CC 30 SCAR 22.7
CC
30
Cov
92%
is_error
16.7
set_error
10.0
_stringify
4.0
CRAP 9.7
« Client/Identity/v3.pm ↑ Summary Client/API/Convert.pm »
1 package Net::OpenStack::Client::Error;
2 9 $Net::OpenStack::Client::Error::VERSION = '0.1.4';
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 our @EXPORT = qw(mkerror);
9  
10 9 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use overload bool => 'is_error', '==' => '_is_equal', '!=' => '_is_not_equal', '""' => '_stringify';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 Readonly::Hash our %ERROR_CODES => {
15 };
16  
17 0 Readonly::Hash our %REVERSE_ERROR_CODES => map {$ERROR_CODES{$_} => $_} keys %ERROR_CODES;
18  
19 =head1 NAME
20  
21 Net::OpenStack::Client::Error is an error class for Net::OpenStack::Client.
22  
23 Boolean logic and (non)-equal operator are overloaded using C<is_error> method.
24 (Use C<==> and C<!=> also for name/message, not C<eq> / C<ne> operators).
25  
26 =head2 Public methods
27  
28 =over
29  
30 =item mkerror
31  
32 A C<Net::OpenStack::Client::Error> factory
33  
34 =cut
35  
36 sub mkerror
37 85 ▶{
Subroutine mkerror: called CC=1 SCAR=0.0
Pod documented
38 85 return Net::OpenStack::Client::Error->new(@_);
39 }
40  
41  
42 =item new
43  
44 Create new error instance from options, e.g. from a (decoded dereferenced) JSON response.
45  
46 Arguments are handled by C<set_error>.
47  
48 =cut
49  
50 sub new
51 86 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod documented
52 86 my $this = shift;
53 86 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0860
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
54 86 my $self = {
55 __errattr => [],
56 };
57 86 bless $self, $class;
58  
59 86 $self->set_error(@_);
60  
61 86 return $self;
62 };
63  
64 =item set_error
65  
66 Process arguments to error
67  
68 =over
69  
70 =item no args/undef: reset the error attribute
71  
72 =item single argument string: convert to an C<Error> instance with message
73  
74 =item single argument hasref/Error instance: make a copy
75  
76 =item single argument/other: set Error message and save original in _orig attribute
77  
78 =item options (more than one arg): set the options
79  
80 =back
81  
82 =cut
83  
84 sub set_error
85 93 ▶{
Subroutine set_error: called CC=10 SCAR=23.0
Pod documented
86 93 my $self = shift;
87  
88 93 my $nrargs = scalar @_;
89  
90 93 my %opts;
91 93 ▶ if ($nrargs == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nrargs == 1) { } 64 29 93
elsif ($nrargs > 1) { } 19 10 29
92 64 my $err = shift;
93 64 my $ref = ref($err);
94  
95 64 ▶ if($ref eq 'Net::OpenStack::Client::Error') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref eq 'Net::OpenStack::Client::Error') { } 1 63 64
elsif ($ref eq 'HASH') { } 3 60 63
elsif (defined $err and $ref eq '') { } 3 57 60
elsif (defined $err) { } 1 56 57
96 1 %opts = map {$_ => $err->{$_}} @{$err->{__errattr}};
97 } elsif ($ref eq 'HASH') {
98 3 %opts = %$err;
99 ▶ } elsif (defined($err) && $ref eq '') {
Condition100%3/3
defined $err and $ref eq ""
!ll&&!rl&&r
5613
MC/DC100%2/2
defined $err and $ref eq ""
defined $err $ref eq ""
Truth table100%3/3
A and B
A= defined $errB= $ref eq ""
ABresult
0X0
100
111
100 3 $opts{message} = $err;
101 } elsif (defined($err)) {
102 1 $opts{message} = "unknown error type $ref, see _orig attribute";
103 1 $opts{_orig} = $err;
104 }
105 } elsif ($nrargs > 1) {
106 19 %opts = @_;
107 }
108  
109  
110 # Wipe current state
111 # Do this after the %opts are build, to allow copies of itself
112 93 foreach my $key (@{$self->{__errattr}}) {
113 8 delete $self->{$key};
114 }
115 93 $self->{__errattr} = [];
116  
117 # sort produces sorted __errattr
118 93 foreach my $key (sort keys %opts) {
119 32 $self->{$key} = $opts{$key};
120 32 push(@{$self->{__errattr}}, $key);
121 }
122  
123 93 return $self;
124 }
125  
126 =item is_error
127  
128 Test if this is an error or not.
129  
130 If an optiona l C<type> argument is passed,
131 test if error name or code is equal to C<type>.
132  
133 A numerical type is compare to the code, a string is compare to the name or message
134  
135 For a set of known errorcodes, a automatic reverse lookup is performed.
136 When e.g. only the error name attribute is set, one can test using a known errorcode.
137  
138 =cut
139  
140 sub is_error
141 198 ▶{
Subroutine is_error: called CC=16 SCAR=28.1
Pod documented
142 198 my ($self, $type, $reverse_lookup) = @_;
143  
144 198 ▶ $reverse_lookup = 1 if ! defined($reverse_lookup);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reverse_lookup 61 137 198
145  
146 198 my $res;
147  
148 198 ▶ if(defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 16 182 198
149 16 my $revtype;
150  
151 16 ▶ if ($type =~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^\d+$/) { } 11 5 16
152 11 ▶ $revtype = $REVERSE_ERROR_CODES{$type} if (exists($REVERSE_ERROR_CODES{$type}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Net::OpenStack::Client::Error::REVERSE_ERROR_CODES{$type} 0 11 11
153 11 ▶ $res = exists($self->{code}) && ($self->{code} == $type);
Condition100%3/3
exists $self->{'code'} && $self->{'code'} == $type
!ll&&!rl&&r
623
MC/DC100%2/2
exists $self->{'code'} && $self->{'code'} == $type
exists $self->{'code'} $self->{'code'} == $type
Truth table100%3/3
A && B
A= exists $self->{'code'}B= $self->{'code'} == $type
ABresult
0X0
100
111
154 } else {
155 5 ▶ $revtype = $ERROR_CODES{$type} if (exists($ERROR_CODES{$type}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Net::OpenStack::Client::Error::ERROR_CODES{$type} 0 5 5
156 5 ▶ $res = (exists($self->{name}) && ($self->{name} eq $type)) || (exists($self->{message}) && ($self->{message} eq $type));
Condition100%9/9
exists $self->{'name'} && $self->{'name'} eq $type || exists $self->{'message'} && $self->{'message'} eq $type
l!l&&r!l&&!r
212
exists $self->{"name"} and $self->{"name"} eq $type
!ll&&!rl&&r
212
exists $self->{"message"} and $self->{"message"} eq $type
!ll&&!rl&&r
111
MC/DC75%3/4
exists $self->{'name'} && $self->{'name'} eq $type || exists $self->{'message'} && $self->{'message'} eq $type
exists $self->{"name"} $self->{"name"} eq $type exists $self->{"message"} $self->{"message"} eq $type
Truth table57%4/7
A and B || C and D
A= exists $self->{"name"}B= $self->{"name"} eq $typeC= exists $self->{"message"}D= $self->{"message"} eq $type
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
157 }
158  
159 # If a reverse known error is found, and it is not yet an error, lookup the reverse
160 # Disable the reverse-lookup here to avoid loop
161 16 ▶ $res = $self->is_error($revtype, 0) if ($reverse_lookup && defined($revtype) && ! $res);
Branch: 1 branch
BranchTrueFalseTotal
if $reverse_lookup and defined $revtype and not $res 0 16 16
Condition33%2/6
$reverse_lookup and defined $revtype and not $res
!ll&&!rl&&r
1600
$reverse_lookup and defined $revtype
!ll&&!rl&&r
0160
MC/DC0%0/3
$reverse_lookup and defined $revtype and not $res
$reverse_lookup defined $revtype not $res
Truth table25%1/4
A and B and not(C)
A= $reverse_lookupB= defined $revtypeC= not $res
ABCresult
0XX0
10X0
1100
1111
162 } else {
163 182 ▶ $res = exists($self->{code}) || exists($self->{name}) || exists($self->{message});
Condition100%6/6
exists $self->{'code'} || exists $self->{'name'} || exists $self->{'message'}
l!l&&r!l&&!r
3013139
exists $self->{"code"} or exists $self->{"name"}
l!l&&r!l&&!r
228152
MC/DC100%3/3
exists $self->{'code'} || exists $self->{'name'} || exists $self->{'message'}
exists $self->{"code"} exists $self->{"name"} exists $self->{'message'}
Truth table100%4/4
A or B || C
A= exists $self->{"code"}B= exists $self->{"name"}C= exists $self->{'message'}
ABCresult
1XX1
01X1
0011
0000
164 }
165  
166 198 ▶ return $res ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 49 149 198
167 }
168  
169 # is_equal for overloading ==
170 # handle == undef (otherwise this would be $self->is_error)
171 sub _is_equal
172 3 ▶{
Subroutine _is_equal: called CC=2 SCAR=7.0
173 # Use shift, looks like a 3rd argument (an empty string) is passed
174 3 my $self = shift;
175 3 my $othererror = shift;
176 3 ▶ return defined($othererror) && $self->is_error($othererror);
Condition66%2/3
defined $othererror && $self->is_error($othererror)
!ll&&!rl&&r
012
MC/DC50%1/2
defined $othererror && $self->is_error($othererror)
defined $othererror $self->is_error($othererror)
Truth table66%2/3
A && B
A= defined $othererrorB= $self->is_error($othererror)
ABresult
0X0
100
111
177 }
178  
179 # inverse is_equal for overloading !=
180 sub _is_not_equal
181 1 ▶{
Subroutine _is_not_equal: called CC=1 SCAR=0.0
182 1 my $self = shift;
183 1 return ! $self->_is_equal(@_);
184 }
185  
186 # _stringify create string for stringification
187 sub _stringify
188 37 ▶{
Subroutine _stringify: called CC=4 SCAR=13.9
189 37 my $self = shift;
190  
191 37 ▶ if ($self->is_error()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_error) { } 26 11 37
192 26 my @fields;
193 26 foreach my $attr (qw(method url name code message)) {
194 130 ▶ push(@fields, $self->{$attr}) if exists ($self->{$attr});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$attr} 30 100 130
195 }
196 26 return "Error ".join(' / ', @fields);
197 } else {
198 11 return "No error";
199 };
200 }
201  
202 =pod
203  
204 =back
205  
206 =cut
207  
208 9 1;
« Client/Identity/v3.pm ↑ Summary Client/API/Convert.pm »