File Coverage

blib/lib/Log/Report/Dispatcher/Try.pm
Criterion Covered Total %
statement 59 62 95.1
branch 17 28 60.7
condition 15 29 51.7
subroutine 19 21 90.4
pod 12 13 92.3
total 122 153 79.7


line stmt bran cond sub pod time code
1             # Copyrights 2007-2017 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5 13     13   72 use warnings;
  13         20  
  13         384  
6 13     13   57 use strict;
  13         22  
  13         317  
7              
8             package Log::Report::Dispatcher::Try;
9 13     13   66 use vars '$VERSION';
  13         21  
  13         527  
10             $VERSION = '1.22';
11              
12 13     13   60 use base 'Log::Report::Dispatcher';
  13         21  
  13         1245  
13              
14 13     13   71 use Log::Report 'log-report', syntax => 'SHORT';
  13         20  
  13         61  
15 13     13   73 use Log::Report::Exception ();
  13         18  
  13         239  
16 13     13   49 use Log::Report::Util qw/%reason_code/;
  13         24  
  13         908  
17              
18              
19             use overload
20 13         62 bool => 'failed'
21             , '""' => 'showStatus'
22 13     13   66 , fallback => 1;
  13         17  
23              
24             #-----------------
25              
26             sub init($)
27 15     15 0 30 { my ($self, $args) = @_;
28 15 50       72 defined $self->SUPER::init($args) or return;
29 15   50     48 $self->{exceptions} = delete $args->{exceptions} || [];
30 15         28 $self->{died} = delete $args->{died};
31 15   50     64 $self->hide($args->{hide} // 'NONE');
32 15   50     52 $self->{on_die} = $args->{on_die} // 'ERROR';
33 15         44 $self;
34             }
35              
36             #-----------------
37              
38             sub died(;$)
39 14     14 1 55 { my $self = shift;
40 14 100       42 @_ ? ($self->{died} = shift) : $self->{died};
41             }
42              
43              
44 4     4 1 6 sub exceptions() { @{shift->{exceptions}} }
  4         15  
45              
46              
47             sub hides($)
48 1 50   1 1 6 { my $h = shift->{hides} or return 0;
49 0 0       0 keys %$h ? $h->{(shift)} : 1;
50             }
51              
52              
53             sub hide(@)
54 15     15 1 26 { my $self = shift;
55 15 50       24 my @h = map { ref $_ eq 'ARRAY' ? @$_ : defined($_) ? $_ : () } @_;
  15 50       63  
56              
57             $self->{hides}
58 15 50 33     106 = @h==0 ? undef
    50 33        
    50          
59             : @h==1 && $h[0] eq 'ALL' ? {} # empty HASH = ALL
60             : @h==1 && $h[0] eq 'NONE' ? undef
61             : +{ map +($_ => 1), @h };
62             }
63              
64              
65 6     6 1 19 sub die2reason() { shift->{on_die} }
66              
67             #-----------------
68              
69             sub log($$$$)
70 12     12 1 29 { my ($self, $opts, $reason, $message, $domain) = @_;
71              
72 12 100       27 unless($opts->{stack})
73 4         51 { my $mode = $self->mode;
74             $opts->{stack} = $self->collectStack
75             if $reason eq 'PANIC'
76             || ($mode==2 && $reason_code{$reason} >= $reason_code{ALERT})
77 4 100 33     42 || ($mode==3 && $reason_code{$reason} >= $reason_code{ERROR});
      33        
      66        
      66        
78             }
79              
80 12   100     50 $opts->{location} ||= '';
81              
82 12         48 my $e = Log::Report::Exception->new
83             ( reason => $reason
84             , report_opts => $opts
85             , message => $message
86             );
87              
88 12         17 push @{$self->{exceptions}}, $e;
  12         26  
89              
90             # $self->{died} ||=
91             # exists $opts->{is_fatal} ? $opts->{is_fatal} : $e->isFatal;
92              
93 12         30 $self;
94             }
95              
96              
97 0     0 1 0 sub reportFatal(@) { $_->throw(@_) for shift->wasFatal }
98 2     2 1 12 sub reportAll(@) { $_->throw(@_) for shift->exceptions }
99              
100             #-----------------
101              
102 5     5 1 108 sub failed() { defined shift->{died}}
103 0     0 1 0 sub success() { ! defined shift->{died}}
104              
105              
106              
107             sub wasFatal(@)
108 19     19 1 3998 { my ($self, %args) = @_;
109 19 100       65 defined $self->{died} or return ();
110              
111 13         62 my $ex = $self->{exceptions}[-1];
112 13 50 66     65 (!$args{class} || $ex->inClass($args{class})) ? $ex : ();
113             }
114              
115              
116             sub showStatus()
117 3     3 1 7 { my $self = shift;
118 3 50       8 my $fatal = $self->wasFatal or return '';
119 3         8 __x"try-block stopped with {reason}: {text}"
120             , reason => $fatal->reason
121             , text => $self->died;
122             }
123              
124             1;