File Coverage

blib/lib/Mail/BIMI/Role/HasError.pm
Criterion Covered Total %
statement 61 66 92.4
branch 8 8 100.0
condition n/a
subroutine 13 14 92.8
pod 7 7 100.0
total 89 95 93.6


line stmt bran cond sub pod time code
1             package Mail::BIMI::Role::HasError;
2             # ABSTRACT: Class to model an error
3             our $VERSION = '3.20210225'; # VERSION
4 30     30   22255 use 5.20.0;
  30         120  
5 30     30   14059 use Moose::Role;
  30         150968  
  30         127  
6 30     30   165647 use Mail::BIMI::Prelude;
  30         85  
  30         253  
7 30     30   24656 use Mail::BIMI::Trait::Cacheable;
  30         91  
  30         1114  
8 30     30   15229 use Mail::BIMI::Trait::CacheSerial;
  30         95  
  30         1113  
9 30     30   15707 use Mail::BIMI::Error;
  30         117  
  30         1275  
10 30     30   255 use Sub::Install;
  30         67  
  30         354  
11              
12             has errors => ( is => 'rw', isa => 'ArrayRef', lazy => 1, default => sub{return []}, traits => ['Cacheable','CacheSerial'] );
13             has warnings => ( is => 'rw', isa => 'ArrayRef', lazy => 1, default => sub{return []}, traits => ['Cacheable'] );
14              
15              
16              
17 26     26 1 1678 sub serialize_errors($self) {
  26         53  
  26         39  
18 26         734 my @data = map {{
19 11         290 code => $_->code,
20             detail => $_->detail,
21             }} $self->errors->@*;
22 26         111 return \@data;
23             }
24              
25              
26 7     7 1 2514 sub deserialize_errors($self,$value) {
  7         26  
  7         14  
  7         11  
27 7         43 foreach my $error ($value->@*) {
28             my $error_object = Mail::BIMI::Error->new(
29             code => $error->{code},
30 2 100       12 ( $error->{detail} ? ( detail => $error->{detail} ) : () ),
31             );
32 2         1200 $self->add_error_object($error_object);
33             }
34             }
35              
36              
37 34     34 1 3336 sub add_error($self,$code,$detail=undef) {
  34         76  
  34         94  
  34         82  
  34         64  
38 34 100       393 my $error = Mail::BIMI::Error->new(
39             code=>$code,
40             ($detail?(detail=>$detail):()),
41             );
42 34         25292 $self->add_error_object($error);
43             }
44              
45              
46 0     0 1 0 sub add_warning($self,$detail) {
  0         0  
  0         0  
  0         0  
47 0         0 push $self->warnings->@*, $detail;
48             }
49              
50              
51 48     48 1 112 sub add_error_object($self,$error) {
  48         92  
  48         90  
  48         84  
52 48 100       204 if ( ref $error eq 'ARRAY' ) {
53 6         39 foreach my $suberror ( $error->@* ){
54 6         34 $self->add_error_object($suberror);
55             }
56             }
57             else {
58 42 100       1172 $self->log_verbose(join(' : ',
59             'Error',
60             $error->code,
61             $error->description,
62             ( $error->detail ? $error->detail : () ),
63             ));
64 42         1234 push $self->errors->@*, $error;
65             }
66             }
67              
68              
69 30     30 1 70 sub error_codes($self) {
  30         79  
  30         64  
70 30         876 my @error_codes = map { $_->code } $self->errors->@*;
  20         561  
71 30         399 return \@error_codes;
72             }
73              
74              
75 48     48 1 434 sub filter_errors($self,$error) {
  48         80  
  48         77  
  48         73  
76 48         1292 return grep { $_->code eq $error } $self->errors->@*;
  48         1146  
77             }
78              
79             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.20210225
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