FreeIPA/Error.pm

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