FreeIPA/Error.pm

filter: Statementstmt 100% 82 / 82 Branchbran 100% 30 / 30 Conditioncond 93.3% 28 / 30 MC/DCmcdc 87.5% 14 / 16 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 97.7% 174 / 178 SCAR 33.7
CC
29
Cov
99%
is_error
16.0
set_error
10.0
_stringify
4.0
CRAP 29.0
« FreeIPA/Common.pm ↑ Summary FreeIPA/API/Convert.pm »
1 package Net::FreeIPA::Error;
2 9 $Net::FreeIPA::Error::VERSION = '3.0.0';
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 our $DUPLICATE_ENTRY => 'DuplicateEntry';
15 9 Readonly our $NOT_FOUND => 'NotFound';
16  
17 9 Readonly::Hash our %ERROR_CODES => {
18 $DUPLICATE_ENTRY => 4002,
19 $NOT_FOUND => 4001,
20 };
21  
22 9 Readonly::Hash our %REVERSE_ERROR_CODES => map {$ERROR_CODES{$_} => $_} keys %ERROR_CODES;
23  
24 =head1 NAME
25  
26 Net::FreeIPA::Error is an error class for Net::FreeIPA.
27  
28 Boolean logic and (non)-equal operator are overloaded using C<is_error> method.
29 (Use C<==> and C<!=> also for name/message, not C<eq> / C<ne> operators).
30  
31 =head2 Public methods
32  
33 =over
34  
35 =item mkerror
36  
37 A C<Net::FreeIPA::Error> factory
38  
39 =cut
40  
41 sub mkerror
42 {
43 43 ▶ return Net::FreeIPA::Error->new(@_);
Subroutine mkerror: called CC=1 SCAR=0.0
Pod documented
44 }
45  
46  
47 =item new
48  
49 Create new error instance from options, e.g. from a (decoded dereferenced) JSON response.
50  
51 Arguments are handled by C<set_error>.
52  
53 =cut
54  
55 sub new
56 {
57 44 ▶ my $this = shift;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
58 44 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0440
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
59 44 my $self = {
60 __errattr => [],
61 };
62 44 bless $self, $class;
63  
64 44 $self->set_error(@_);
65  
66 44 return $self;
67 };
68  
69 =item set_error
70  
71 Process arguments to error
72  
73 =over
74  
75 =item no args/undef: reset the error attribute
76  
77 =item single argument string: convert to an C<Error> instance with message
78  
79 =item single argument hasref/Error instance: make a copy
80  
81 =item single argument/other: set Error message and save original in _orig attribute
82  
83 =item options (more than one arg): set the options
84  
85 =back
86  
87 =cut
88  
89 sub set_error
90 {
91 51 ▶ my $self = shift;
Subroutine set_error: called CC=10 SCAR=23.0
Pod documented
92  
93 51 my $nrargs = scalar @_;
94  
95 51 my %opts;
96 51 ▶ if ($nrargs == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nrargs == 1) { } 10 41 51
elsif ($nrargs > 1) { } 31 10 41
97 10 my $err = shift;
98 10 my $ref = ref($err);
99  
100 10 ▶ if($ref eq 'Net::FreeIPA::Error') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref eq 'Net::FreeIPA::Error') { } 1 9 10
elsif ($ref eq 'HASH') { } 2 7 9
elsif (defined $err and $ref eq '') { } 3 4 7
elsif (defined $err) { } 1 3 4
101 1 %opts = map {$_ => $err->{$_}} @{$err->{__errattr}};
102 } elsif ($ref eq 'HASH') {
103 2 %opts = %$err;
104 ▶ } elsif (defined($err) && $ref eq '') {
Condition100%3/3
defined $err and $ref eq ""
!ll&&!rl&&r
313
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
105 3 $opts{message} = $err;
106 } elsif (defined($err)) {
107 1 $opts{message} = "unknown error type $ref, see _orig attribute";
108 1 $opts{_orig} = $err;
109 }
110 } elsif ($nrargs > 1) {
111 31 %opts = @_;
112 }
113  
114  
115 # Wipe current state
116 # Do this after the %opts are build, to allow copies of itself
117 51 foreach my $key (@{$self->{__errattr}}) {
118 7 delete $self->{$key};
119 }
120 51 $self->{__errattr} = [];
121  
122 # sort produces sorted __errattr
123 51 foreach my $key (sort keys %opts) {
124 40 $self->{$key} = $opts{$key};
125 40 push(@{$self->{__errattr}}, $key);
126 }
127  
128 51 return $self;
129 }
130  
131 =item is_error
132  
133 Test if this is an error or not.
134  
135 If an optiona l C<type> argument is passed,
136 test if error name or code is equal to C<type>.
137  
138 A numerical type is compare to the code, a string is compare to the name or message
139  
140 For a set of known errorcodes, a automatic reverse lookup is performed.
141 When e.g. only the error name attribute is set, one can test using a known errorcode.
142  
143 =cut
144  
145 sub is_error
146 {
147 130 ▶ my ($self, $type, $reverse_lookup) = @_;
Subroutine is_error: called CC=16 SCAR=27.7
Pod documented
148  
149 130 ▶ $reverse_lookup = 1 if ! defined($reverse_lookup);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reverse_lookup 80 50 130
150  
151 130 my $res;
152  
153 130 ▶ if(defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 46 84 130
154 46 my $revtype;
155  
156 46 ▶ if ($type =~ m/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^\d+$/) { } 25 21 46
157 25 ▶ $revtype = $REVERSE_ERROR_CODES{$type} if (exists($REVERSE_ERROR_CODES{$type}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Net::FreeIPA::Error::REVERSE_ERROR_CODES{$type} 14 11 25
158 25 ▶ $res = exists($self->{code}) && ($self->{code} == $type);
Condition100%3/3
exists $self->{'code'} && $self->{'code'} == $type
!ll&&!rl&&r
11410
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
159 } else {
160 21 ▶ $revtype = $ERROR_CODES{$type} if (exists($ERROR_CODES{$type}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Net::FreeIPA::Error::ERROR_CODES{$type} 16 5 21
161 21 ▶ $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
9111
exists $self->{"name"} and $self->{"name"} eq $type
!ll&&!rl&&r
939
exists $self->{"message"} and $self->{"message"} eq $type
!ll&&!rl&&r
1011
MC/DC100%4/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 table71%5/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
162 }
163  
164 # If a reverse known error is found, and it is not yet an error, lookup the reverse
165 # Disable the reverse-lookup here to avoid loop
166 46 ▶ $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 12 34 46
Condition100%6/6
$reverse_lookup and defined $revtype and not $res
!ll&&!rl&&r
28612
$reverse_lookup and defined $revtype
!ll&&!rl&&r
121618
MC/DC100%3/3
$reverse_lookup and defined $revtype and not $res
$reverse_lookup defined $revtype not $res
Truth table100%4/4
A and B and not(C)
A= $reverse_lookupB= defined $revtypeC= not $res
ABCresult
0XX0
10X0
1100
1111
167 } else {
168 84 ▶ $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
301935
exists $self->{"code"} or exists $self->{"name"}
l!l&&r!l&&!r
21954
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
169 }
170  
171 130 ▶ return $res ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 77 53 130
172 }
173  
174 =item is_duplicate
175  
176 Test if this is a DuplicateEntry error
177  
178 =cut
179  
180 sub is_duplicate
181 {
182 4 ▶ my ($self) = @_;
Subroutine is_duplicate: called CC=1 SCAR=0.0
Pod documented
183  
184 4 return $self->is_error($DUPLICATE_ENTRY);
185 }
186  
187 =item is_not_found
188  
189 Test if this is a NotFound error
190  
191 =cut
192  
193 sub is_not_found
194 {
195 4 ▶ my ($self) = @_;
Subroutine is_not_found: called CC=1 SCAR=0.0
Pod documented
196  
197 4 return $self->is_error($NOT_FOUND);
198 }
199  
200 # is_equal for overloading ==
201 sub _is_equal
202 {
203 # Use shift, looks like a 3rd argument (an empty string) is passed
204 7 ▶ my $self = shift;
Subroutine _is_equal: called CC=1 SCAR=0.0
205 7 return $self->is_error(shift);
206 }
207  
208 # inverse is_equal for overloading !=
209 sub _is_not_equal
210 {
211 1 ▶ my $self = shift;
Subroutine _is_not_equal: called CC=1 SCAR=0.0
212 1 return ! $self->_is_equal(@_);
213 }
214  
215 # _stringify create string for stringification
216 sub _stringify
217 {
218 38 ▶ my $self = shift;
Subroutine _stringify: called CC=4 SCAR=13.9
219  
220 38 ▶ if ($self->is_error()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_error) { } 27 11 38
221 27 my @fields;
222 27 foreach my $attr (qw(name code message)) {
223 81 ▶ push(@fields, $self->{$attr}) if exists ($self->{$attr});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$attr} 28 53 81
224 }
225 27 return "Error ".join('/', @fields);
226 } else {
227 11 return "No error";
228 };
229 }
230  
231 =pod
232  
233 =back
234  
235 =cut
236  
237 9 1;
« FreeIPA/Common.pm ↑ Summary FreeIPA/API/Convert.pm »