File Coverage

blib/lib/EntityModel/Error.pm
Criterion Covered Total %
statement 15 39 38.4
branch 0 10 0.0
condition 0 4 0.0
subroutine 5 10 50.0
pod 1 1 100.0
total 21 64 32.8


line stmt bran cond sub pod time code
1             package EntityModel::Error;
2             $EntityModel::Error::VERSION = '0.016';
3 1     1   6 use strict;
  1         2  
  1         30  
4 1     1   4 use warnings;
  1         1  
  1         31  
5              
6             =head1 NAME
7              
8             EntityModel::Error - generic error object
9              
10             =head1 VERSION
11              
12             Version 0.016
13              
14             =head1 DESCRIPTION
15              
16             Uses some overload tricks and C< AUTOLOAD > to allow chained method calls without needing to wrap in eval.
17              
18             =head1 METHODS
19              
20             =cut
21              
22 1     1   4 use EntityModel::Log ':all';
  1         2  
  1         100  
23 1     1   28825 use Data::Dumper;
  1         22619  
  1         176  
24              
25             use overload
26             'bool' => sub {
27 0     0     my $self = shift;
28 0   0       logWarning('Error: [%s], chain was [%s]',
29             Data::Dumper::Dumper($self->{message}),
30             join(',', map {
31 0           $_->{method} // 'unknown'
32 0           } @{ $self->{chain} })
33             );
34 0           return 0;
35             },
36 0     0     'ne' => sub { 1 },
37 0     0     'eq' => sub { 0 },
38 1     1   9 'fallback' => 1;
  1         3  
  1         37  
39              
40             =head2 new
41              
42             Instantiate a new L object. Takes the following parameters:
43              
44             =over 4
45              
46             =item * $parent - the parent error which raised this one
47              
48             =item * $msg - error message, string
49              
50             =item * $opt (optional) - hashref of options
51              
52             =back
53              
54             =cut
55              
56             sub new {
57 0     0 1   my ($class, $parent, $msg, $opt) = @_;
58 0 0         $msg = $parent if @_ < 3;
59 0   0       $opt ||= { };
60              
61 0 0         logWarning($msg) if $opt->{warning};
62 0 0         logError($msg) if $opt->{error};
63 0           logInfo("Had error [%s] from %S", $msg);
64              
65 0           my $self = bless {
66             message => $msg,
67             parent => $parent,
68             chain => [ ]
69             }, $class;
70 0           return $self;
71             }
72              
73             our $AUTOLOAD;
74              
75             sub AUTOLOAD {
76 0     0     my $self = shift;
77 0           my ($method) = $AUTOLOAD;
78 0           $method =~ s/^.*:://g;
79 0 0         return if $method eq 'DESTROY';
80              
81             logWarning('Bad method [%s] called in error, original message [%s] with object [%s]',
82             $method,
83             $self->{message},
84             $self->{parent}
85 0 0         ) unless eval { $self->{parent}->can($method) };
  0            
86              
87 0           push @{$self->{chain}}, {method => $method };
  0            
88 0           return $self;
89             }
90              
91             1;
92              
93             __END__