File Coverage

blib/lib/Log/Report/Dispatcher/Try.pm
Criterion Covered Total %
statement 59 62 95.1
branch 16 28 57.1
condition 13 29 44.8
subroutine 19 21 90.4
pod 12 13 92.3
total 119 153 77.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   83 use warnings;
  13         30  
  13         401  
6 13     13   69 use strict;
  13         26  
  13         366  
7              
8             package Log::Report::Dispatcher::Try;
9 13     13   69 use vars '$VERSION';
  13         28  
  13         583  
10             $VERSION = '1.21';
11              
12 13     13   78 use base 'Log::Report::Dispatcher';
  13         25  
  13         1046  
13              
14 13     13   79 use Log::Report 'log-report', syntax => 'SHORT';
  13         30  
  13         77  
15 13     13   76 use Log::Report::Exception ();
  13         29  
  13         234  
16 13     13   57 use Log::Report::Util qw/%reason_code/;
  13         26  
  13         963  
17              
18              
19             use overload
20 13         74 bool => 'failed'
21             , '""' => 'showStatus'
22 13     13   73 , fallback => 1;
  13         23  
23              
24             #-----------------
25              
26             sub init($)
27 15     15 0 36 { my ($self, $args) = @_;
28 15 50       59 defined $self->SUPER::init($args) or return;
29 15   50     69 $self->{exceptions} = delete $args->{exceptions} || [];
30 15         32 $self->{died} = delete $args->{died};
31 15   50     90 $self->hide($args->{hide} // 'NONE');
32 15   50     65 $self->{on_die} = $args->{on_die} // 'ERROR';
33 15         49 $self;
34             }
35              
36             #-----------------
37              
38             sub died(;$)
39 14     14 1 52 { my $self = shift;
40 14 100       48 @_ ? ($self->{died} = shift) : $self->{died};
41             }
42              
43              
44 4     4 1 7 sub exceptions() { @{shift->{exceptions}} }
  4         16  
45              
46              
47             sub hides($)
48 1 50   1 1 8 { 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 27 { my $self = shift;
55 15 50       28 my @h = map { ref $_ eq 'ARRAY' ? @$_ : defined($_) ? $_ : () } @_;
  15 50       77  
56              
57             $self->{hides}
58 15 50 33     153 = @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 27 sub die2reason() { shift->{on_die} }
66              
67             #-----------------
68              
69             sub log($$$$)
70 12     12 1 34 { my ($self, $opts, $reason, $message, $domain) = @_;
71              
72 12 100       37 unless($opts->{stack})
73 4         21 { 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 50 33     44 || ($mode==3 && $reason_code{$reason} >= $reason_code{ERROR});
      33        
      33        
      33        
78             }
79              
80 12   100     41 $opts->{location} ||= '';
81              
82 12         57 my $e = Log::Report::Exception->new
83             ( reason => $reason
84             , report_opts => $opts
85             , message => $message
86             );
87              
88 12         43 push @{$self->{exceptions}}, $e;
  12         32  
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 43 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 4058 { my ($self, %args) = @_;
109 19 100       80 defined $self->{died} or return ();
110              
111 13         31 my $ex = $self->{exceptions}[-1];
112 13 50 66     66 (!$args{class} || $ex->inClass($args{class})) ? $ex : ();
113             }
114              
115              
116             sub showStatus()
117 3     3 1 7 { my $self = shift;
118 3 50       7 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;