BIMI/Role/HasError.pm

filter: Statementstmt 95.6% 44 / 46 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 14 / 16 Podpod 100% 7 / 7 totaltotal 94.8% 73 / 77 CC 8 SCAR 7.4
CC
8
Cov
96%
add_error_object
4.0
deserialize_errors
3.0
add_error
3.0
CRAP 2.1
« BIMI/Options.pm ↑ Summary BIMI/CacheBackend/File.pm »
1 package Mail::BIMI::Role::HasError;
2 # ABSTRACT: Class to model an error
3 30 our $VERSION = '3.20260302'; # VERSION
4 30 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Mail::BIMI::Prelude;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Mail::BIMI::Trait::Cacheable;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Mail::BIMI::Trait::CacheSerial;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Mail::BIMI::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 98 ▶has errors => ( is => 'rw', isa => 'ArrayRef', lazy => 1, default => sub{return []}, traits => ['Cacheable','CacheSerial'] );
Subroutine __ANON__: called CC=1 SCAR=0.0
13 0 ▶has warnings => ( is => 'rw', isa => 'ArrayRef', lazy => 1, default => sub{return []}, traits => ['Cacheable'] );
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15  
16  
17 ▶sub serialize_errors($self) {
Subroutine serialize_errors: called CC=1 SCAR=0.0
Pod documented
18 14 my @data = map {{
19 5 code => $_->code,
20 detail => $_->detail,
21 }} $self->errors->@*;
22 14 return \@data;
23 }
24  
25  
26 ▶sub deserialize_errors($self,$value) {
Subroutine deserialize_errors: called CC=3 SCAR=11.0
Pod documented
27 7 foreach my $error ($value->@*) {
28 my $error_object = Mail::BIMI::Error->new(
29 code => $error->{code},
30 2 ▶ ( $error->{detail} ? ( detail => $error->{detail} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$error->{'detail'} ? : 1 1 2
31 );
32 2 $self->add_error_object($error_object);
33 }
34 }
35  
36  
37 25 ▶sub add_error($self,$code,$detail=undef) {
Subroutine add_error: called CC=3 SCAR=11.0
Pod documented
38 25 ▶ my $error = Mail::BIMI::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
$detail ? : 4 21 25
39 code=>$code,
40 ($detail?(detail=>$detail):()),
41 );
42 25 $self->add_error_object($error);
43 }
44  
45  
46 ▶sub add_warning($self,$detail) {
Subroutine add_warning: not called CC=1 SCAR=6.9
Pod documented
47 0 push $self->warnings->@*, $detail;
48 }
49  
50  
51 ▶sub add_error_object($self,$error) {
Subroutine add_error_object: called CC=4 SCAR=13.9
Pod documented
52 33 ▶ if ( ref $error eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error eq 'ARRAY') { } 3 30 33
53 3 foreach my $suberror ( $error->@* ){
54 3 $self->add_error_object($suberror);
55 }
56 }
57 else {
58 30 ▶ $self->log_verbose(join(' : ',
Branch: 1 branch
BranchTrueFalseTotal
$error->detail ? : 5 25 30
59 'Error',
60 $error->code,
61 $error->description,
62 ( $error->detail ? $error->detail : () ),
63 ));
64 30 push $self->errors->@*, $error;
65 }
66 }
67  
68  
69 ▶sub error_codes($self) {
Subroutine error_codes: called CC=1 SCAR=0.0
Pod documented
70 31 my @error_codes = map { $_->code } $self->errors->@*;
71 31 return \@error_codes;
72 }
73  
74  
75 ▶sub filter_errors($self,$error) {
Subroutine filter_errors: called CC=1 SCAR=0.0
Pod documented
76 22 return grep { $_->code eq $error } $self->errors->@*;
77 }
78  
79 30 1;
80  
81 __END__
82  
83 =pod
84  
85 =encoding UTF-8
86  
87 =head1 NAME
88  
89 Mail::BIMI::Role::HasError - Class to model an error
90  
91 =head1 VERSION
92  
93 version 3.20260302
94  
95 =head1 DESCRIPTION
96  
97 Role for handling validation errors and warnings
98  
99 =head1 METHODS
100  
101 =head2 I<serialize_errors()>
102  
103 Serialize the errors property for cache storage
104  
105 =head2 I<deserialize_errors($value)>
106  
107 De-serialize the errors property for cache storage
108  
109 =head2 I<add_error($code,$detail)>
110  
111 Add an error with the given code and optional detail to the current operation.
112  
113 =head2 I<add_warning($detail)>
114  
115 Add a warning which may be returned to a validator.
116  
117 =head2 I<add_error_object($error)>
118  
119 Add an existing error object, or objects, to the current operation
120  
121 =head2 I<error_codes>
122  
123 Return an ArrayRef of current error codes
124  
125 =head2 I<filter_errors($error)>
126  
127 Return error(s) matching the given error code
128  
129 =head1 REQUIRES
130  
131 =over 4
132  
133 =item * L<Mail::BIMI::Error|Mail::BIMI::Error>
134  
135 =item * L<Mail::BIMI::Prelude|Mail::BIMI::Prelude>
136  
137 =item * L<Mail::BIMI::Trait::CacheSerial|Mail::BIMI::Trait::CacheSerial>
138  
139 =item * L<Mail::BIMI::Trait::Cacheable|Mail::BIMI::Trait::Cacheable>
140  
141 =item * L<Moose::Role|Moose::Role>
142  
143 =item * L<Sub::Install|Sub::Install>
144  
145 =back
146  
147 =head1 AUTHOR
148  
149 Marc Bradshaw <marc@marcbradshaw.net>
150  
151 =head1 COPYRIGHT AND LICENSE
152  
153 This software is copyright (c) 2020 by Marc Bradshaw.
154  
155 This is free software; you can redistribute it and/or modify it under
156 the same terms as the Perl 5 programming language system itself.
157  
158 =cut
« BIMI/Options.pm ↑ Summary BIMI/CacheBackend/File.pm »