blib/lib/Class/Throwable.pm

filter: Statementstmt 100% 112 / 112 Branchbran 98.0% 51 / 52 Conditioncond 66.6% 14 / 21 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 100% 19 / 19 Podpod 100% 10 / 10 totaltotal 93.3% 212 / 227 CC 37 SCAR 19.1
CC
37
Cov
96%
import
14.1
toString
10.0
throw
4.0
CRAP 6.8
↑ Summary
1  
2 package Class::Throwable;
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '0.13';
8  
9 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 our $DEFAULT_VERBOSITY = 1;
12  
13 7 my %VERBOSITY;
14  
15 # allow the creation of exceptions
16 # without having to actually create
17 # a package for them
18 15 ▶sub import {
Subroutine import: called CC=14 SCAR=26.5
19 15 my $class = shift;
20 15 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 13 15
21 13 ▶ if ($_[0] eq 'VERBOSE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] eq 'VERBOSE') { } 5 8 13
elsif ($_[0] eq 'retrofit') { } 3 5 8
22 5 ▶ (defined $_[1]) || die "You must specify a level of verbosity with Class::Throwable\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] or die "You must specify a level of verbosity with Class::Throwable\n" 4 1 5
23 # make sure its not a refernce
24 4 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
25 # and then store it
26 4 $VERBOSITY{$class} = $_[1];
27 }
28 elsif ($_[0] eq 'retrofit') {
29 3 ▶ (defined $_[1]) || die "You must specify a module for Class::Throwable to retrofit\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] or die "You must specify a module for Class::Throwable to retrofit\n" 2 1 3
30 2 my $package = $_[1];
31 2 ▶ my $retrofitter = sub { Class::Throwable->throw(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 2 ▶ $retrofitter = $_[2] if defined $_[2] && ref($_[2]) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] and ref $_[2] eq "CODE" 1 1 2
Condition66%2/3
defined $_[2] and ref $_[2] eq "CODE"
!ll&&!rl&&r
101
MC/DC50%1/2
defined $_[2] and ref $_[2] eq "CODE"
defined $_[2] ref $_[2] eq "CODE"
Truth table66%2/3
A and B
A= defined $_[2]B= ref $_[2] eq "CODE"
ABresult
0X0
100
111
33 2 eval {
34 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 *{"${package}::die"} = $retrofitter;
36 };
37 2 ▶ die "Could not retrofit '$package' with Class::Throwable : $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
38 }
39 else {
40 5 ▶ ($class eq 'Class::Throwable')
Branch: 1 branch
BranchTrueFalseTotal
$class eq "Class::Throwable" or die "Inline Exceptions can only be created with Class::Throwable\n" 4 1 5
41 || die "Inline Exceptions can only be created with Class::Throwable\n";
42 4 my @exceptions = @_;
43 4 foreach my $exception (@exceptions) {
44 7 ▶ next unless $exception;
Branch: 1 branch
BranchTrueFalseTotal
unless $exception 3 4 7
45 4 eval "package ${exception}; \@${exception}::ISA = qw(Class::Throwable);";
46 4 ▶ die "An error occured while constructing Class::Throwable exception ($exception) : $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
47 }
48 }
49 }
50  
51 # overload the stringify operation
52 7 ▶use overload q|""| => "toString", fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 # a class method to set the verbosity
55 # of inline exceptions
56 3 ▶sub setVerbosity {
Subroutine setVerbosity: called CC=3 SCAR=11.0
Pod documented
57 3 my ($class, $verbosity) = @_;
58 3 ▶ (!ref($class)) || die "setVerbosity is a class method only, it cannot be used on an instance\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $class and die "setVerbosity is a class method only, it cannot be used on an instance\n" 1 2 3
59 2 ▶ (defined($verbosity)) || die "You must specify a level of verbosity with Class::Throwable\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $verbosity or die "You must specify a level of verbosity with Class::Throwable\n" 1 1 2
60 1 $VERBOSITY{$class} = $verbosity;
61 }
62  
63 # create an exception without
64 # any stack trace information
65 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
66 2 my ($class, $message, $sub_exception) = @_;
67 2 my $exception = {};
68 2 ▶ bless($exception, ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
69 2 $exception->_init($message, $sub_exception);
70 2 return $exception;
71 }
72  
73 # throw an exception with this
74 19 ▶sub throw {
Subroutine throw: called CC=4 SCAR=13.9
Pod documented
75 19 my ($class, $message, $sub_exception) = @_;
76 # if i am being re-thrown, then just die with the class
77 19 ▶ if (blessed($class) && $class->isa("Class::Throwable")) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class) and $class->isa("Class::Throwable")) 2 17 19
Condition66%2/3
builtin::blessed($class) and $class->isa("Class::Throwable")
!ll&&!rl&&r
1702
MC/DC50%1/2
builtin::blessed($class) and $class->isa("Class::Throwable")
builtin::blessed($class) $class->isa("Class::Throwable")
Truth table66%2/3
A and B
A= builtin::blessed($class)B= $class->isa("Class::Throwable")
ABresult
0X0
100
111
78 # first make sure we have a stack trace, if we
79 # don't then we were likely created with 'new'
80 # and not 'throw', and so we need to gather the
81 # stack information from here
82 2 ▶ $class->_initStackTrace() unless my @s = $class->getStackTrace();
Branch: 1 branch
BranchTrueFalseTotal
unless my(@s) = $class->getStackTrace 1 1 2
83 2 die $class;
84 }
85 # otherwise i am being thrown for the first time so
86 # create a new 'me' and then die after i am blessed
87 17 my $exception = {};
88 17 bless($exception, $class);
89 17 $exception->_init($message, $sub_exception);
90 # init our stack trace
91 17 $exception->_initStackTrace();
92 17 die $exception;
93 }
94  
95 ## initializers
96  
97 19 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
98 19 my ($self, $message, $sub_exception) = @_;
99 # the sub-exception is another exception
100 # which has already been caught, and is
101 # the cause of this exception being thrown
102 # so we dont want to loose that information
103 # so we store it here
104 # NOTE:
105 # we do not enforce the type of exception here
106 # becuase it is possible this was thrown by
107 # perl itself and therefore could be a string
108 19 $self->{sub_exception} = $sub_exception;
109 19 ▶ $self->{message} = $message || "An ". ref($self) . " Exception has been thrown";
Condition100%2/2
$message || 'An ' . ref($self) . ' Exception has been thrown'
l!l
154
MC/DC100%1/1
$message || 'An ' . ref($self) . ' Exception has been thrown'
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
110 19 $self->{stack_trace} = [];
111 }
112  
113 18 ▶sub _initStackTrace {
Subroutine _initStackTrace: called CC=3 SCAR=11.0
114 18 my ($self) = @_;
115 18 my @stack_trace;
116 # these are the 10 values returned from caller():
117 # $package, $filename, $line, $subroutine, $hasargs,
118 # $wantarray, $evaltext, $is_require, $hints, $bitmask
119 # we do not bother to capture the last two as they are
120 # subject to change and not meant for internal use
121 {
122 18 package DB;
123 18 my $i = 1;
124 18 my @c;
125 18 ▶ while (@c = caller($i++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c = caller $i++) 51 18 69
126 # dont bother to get our caller
127 51 ▶ next if $c[3] =~ /Class\:\:Throwable\:\:throw/;
Branch: 1 branch
BranchTrueFalseTotal
if $c[3] =~ /Class\:\:Throwable\:\:throw/ 18 33 51
128 33 push @stack_trace, [ @c[0 .. 7] ];
129 }
130 }
131 18 $self->{stack_trace} = \@stack_trace;
132 }
133  
134 # accessors
135  
136 15 ▶sub hasSubException {
Subroutine hasSubException: called CC=2 SCAR=6.9
Pod documented
137 15 my ($self) = @_;
138 15 ▶ return defined $self->{sub_exception} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'sub_exception'} ? : 8 7 15
139 }
140  
141 8 ▶sub getSubException {
Subroutine getSubException: called CC=1 SCAR=0.0
Pod documented
142 8 my ($self) = @_;
143 8 return $self->{sub_exception};
144 }
145  
146 9 ▶sub getMessage {
Subroutine getMessage: called CC=1 SCAR=0.0
Pod documented
147 9 my ($self) = @_;
148 9 return $self->{"message"};
149 }
150  
151 6 ▶sub getStackTrace {
Subroutine getStackTrace: called CC=2 SCAR=6.9
Pod documented
152 6 my ($self) = @_;
153 return wantarray ?
154 4 @{$self->{stack_trace}}
155 :
156 6 ▶ $self->{stack_trace};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 2 6
157 }
158  
159 12 ▶sub stackTraceToString {
Subroutine stackTraceToString: called CC=4 SCAR=13.9
Pod documented
160 12 my ($self, $depth) = @_;
161 12 my @output;
162 12 ▶ $depth ||= 1;
Condition100%2/2
$depth ||= 1
l!l
111
MC/DC100%1/1
$depth ||= 1
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
163 12 my $indent = " " x $depth;
164 12 foreach my $frame (@{$self->{stack_trace}}) {
165 21 my ($package, $filename, $line, $subroutine) = @{$frame};
166 21 ▶ $subroutine = "${package}::${subroutine}" if ($subroutine eq '(eval)');
Branch: 1 branch
BranchTrueFalseTotal
if $subroutine eq "(eval)" 15 6 21
167 21 push @output, "$indent|--[ $subroutine called in $filename line $line ]"
168 }
169 12 return (join "\n" => @output);
170 }
171  
172 16 ▶sub toString {
Subroutine toString: called CC=10 SCAR=23.0
Pod documented
173 16 my ($self, $verbosity, $depth) = @_;
174 16 ▶ unless (defined $verbosity) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $verbosity) 9 7 16
175 9 ▶ if (exists $VERBOSITY{ref($self)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $VERBOSITY{ref $self}) { } 7 2 9
176 7 $verbosity = $VERBOSITY{ref($self)};
177 }
178 else {
179 2 $verbosity = $DEFAULT_VERBOSITY;
180 }
181 }
182 # get out of here quick if
183 # exception handling is off
184 16 ▶ return "" if $verbosity <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $verbosity <= 0 1 15 16
185 # otherwise construct our output
186 15 my $output = ref($self) . " : " . $self->{"message"};
187 # if we VERBOSE is set to 1, then
188 # we just return the message
189 15 ▶ return $output if $verbosity <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbosity <= 1 4 11 15
190 11 ▶ $depth ||= 1;
Condition100%2/2
$depth ||= 1
l!l
47
MC/DC100%1/1
$depth ||= 1
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
191 11 ▶ if ($depth > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 1) 4 7 11
192 4 $output = (" " x ($depth - 1)) . "+ $output";
193 4 $depth++;
194 }
195 # however, if VERBOSE is 2 or above
196 # then we include the stack trace
197 11 $output .= "\n" . (join "\n" => $self->stackTraceToString($depth)) . "\n";
198 # now we gather any sub-exceptions too
199 11 ▶ if ($self->hasSubException()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasSubException) 5 6 11
200 5 my $e = $self->getSubException();
201 # make sure the sub-exception is one
202 # of our objects, and ....
203 5 ▶ if (blessed($e) && $e->isa("Class::Throwable")) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($e) and $e->isa('Class::Throwable')) { } 4 1 5
Condition66%2/3
builtin::blessed($e) and $e->isa("Class::Throwable")
!ll&&!rl&&r
104
MC/DC50%1/2
builtin::blessed($e) and $e->isa("Class::Throwable")
builtin::blessed($e) $e->isa("Class::Throwable")
Truth table66%2/3
A and B
A= builtin::blessed($e)B= $e->isa("Class::Throwable")
ABresult
0X0
100
111
204 # deal with it appropriately
205 4 $output .= $e->toString($verbosity, $depth + 1);
206 }
207 # otherwise ...
208 else {
209 # just stringify it
210 1 $output .= (" " x ($depth)) . "+ $e";
211 }
212 }
213 11 return $output;
214 }
215  
216 6 ▶sub stringValue {
Subroutine stringValue: called CC=1 SCAR=0.0
Pod documented
217 6 my ($self) = @_;
218 6 return overload::StrVal($self);
219 }
220  
221 7 1;
222  
223 __END__
↑ Summary