File Coverage

blib/lib/Log/Report/Exception.pm
Criterion Covered Total %
statement 48 55 87.2
branch 8 14 57.1
condition 2 11 18.1
subroutine 17 20 85.0
pod 10 11 90.9
total 85 111 76.5


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   68 use warnings;
  13         24  
  13         362  
6 13     13   74 use strict;
  13         18  
  13         315  
7              
8             package Log::Report::Exception;
9 13     13   50 use vars '$VERSION';
  13         17  
  13         499  
10             $VERSION = '1.22';
11              
12              
13 13     13   65 use Log::Report 'log-report';
  13         19  
  13         56  
14 13     13   61 use Log::Report::Util qw/is_fatal to_html/;
  13         22  
  13         514  
15 13     13   59 use POSIX qw/locale_h/;
  13         20  
  13         53  
16 13     13   1422 use Scalar::Util qw/blessed/;
  13         29  
  13         824  
17              
18              
19             use overload
20             '""' => 'toString'
21 14     14   33 , 'bool' => sub {1} # avoid accidental serialization of message
22 13     13   67 , fallback => 1;
  13         26  
  13         77  
23              
24              
25             sub new($@)
26 16     16 1 63 { my ($class, %args) = @_;
27 16   50     33 $args{report_opts} ||= {};
28 16         112 bless \%args, $class;
29             }
30              
31              
32 0     0 1 0 sub report_opts() {shift->{report_opts}}
33              
34              
35             sub reason(;$)
36 7     7 1 1430 { my $self = shift;
37 7 50       29 @_ ? $self->{reason} = uc(shift) : $self->{reason};
38             }
39              
40              
41 5     5 1 16 sub isFatal() { is_fatal shift->{reason} }
42              
43              
44             sub message(;$)
45 12     12 1 1188 { my $self = shift;
46 12 50       97 @_ or return $self->{message};
47              
48 0         0 my $msg = shift;
49 0 0 0     0 blessed $msg && $msg->isa('Log::Report::Message')
50             or panic "message() of exception expects Log::Report::Message";
51 0         0 $self->{message} = $msg;
52             }
53              
54              
55 2     2 1 7 sub inClass($) { $_[0]->message->inClass($_[1]) }
56              
57              
58             sub throw(@)
59 3     3 1 8 { my $self = shift;
60 3 100       9 my $opts = @_ ? { %{$self->{report_opts}}, @_ } : $self->{report_opts};
  1         5  
61              
62 3         4 my $reason;
63 3 100       7 if($reason = delete $opts->{reason})
64 1         2 { $self->{reason} = $reason;
65             $opts->{is_fatal} = is_fatal $reason
66 1 50       4 unless exists $opts->{is_fatal};
67             }
68             else
69 2         4 { $reason = $self->{reason};
70             }
71              
72 3   33     11 $opts->{stack} ||= Log::Report::Dispatcher->collectStack;
73 3         9 report $opts, $reason, $self;
74             }
75              
76             # where the throw is handled is not interesting
77 6     6 0 30 sub PROPAGATE($$) {shift}
78              
79              
80             sub toString(;$)
81 2     2 1 724 { my ($self, $locale) = @_;
82 2         6 my $msg = $self->message;
83 2 50       14 lc($self->{reason}).': '.(ref $msg ? $msg->toString($locale) : $msg)."\n";
84             }
85              
86              
87 0     0 1   sub toHTML(;$) { to_html($_[0]->toString($_[1])) }
88              
89              
90             sub print(;$)
91 0     0 1   { my $self = shift;
92 0   0       (shift || *STDERR)->print($self->toString);
93             }
94              
95             1;