File Coverage

blib/lib/Error/Hierarchy/Mixin.pm
Criterion Covered Total %
statement 20 29 68.9
branch 1 6 16.6
condition 0 3 0.0
subroutine 7 9 77.7
pod 0 2 0.0
total 28 49 57.1


line stmt bran cond sub pod time code
1 2     2   32359 use 5.008;
  2         8  
  2         95  
2 2     2   11 use strict;
  2         3  
  2         67  
3 2     2   11 use warnings;
  2         4  
  2         125  
4              
5             package Error::Hierarchy::Mixin;
6             BEGIN {
7 2     2   51 $Error::Hierarchy::Mixin::VERSION = '1.103530';
8             }
9             # ABSTRACT: Provides often-used exception-related methods
10 2     2   11 use Error; # to get $Error::Depth
  2         4  
  2         14  
11              
12             BEGIN {
13             *CORE::GLOBAL::die = sub (@) {
14              
15             # Error.pm die()s as well, but we don't want an endless recursion.
16 0 0 0 0   0 CORE::die(@_) if (caller)[0] eq 'Error' || ref $_[0];
17 0         0 local $Error::Depth = $Error::Depth + 1; # skip this level
18 0         0 throw Error::Hierarchy::Internal::CustomMessage(
19             custom_message => join(' ', @_),);
20 2     2   729 };
21             }
22              
23             # Any class that wants to throw an exception can simply use or inherit from
24             # this module and call 'throw Error::Whatever' without having to 'require' it
25             # first. By putting the throw() method in UNIVERSAL:: we catch method calls on
26             # exception classes that haven't been loaded yet. We load the class, then
27             # throw the exception.
28             sub UNIVERSAL::throw {
29              
30             # use Data::Dumper; print Dumper \@_; exit if ++(our $cnt) > 5;
31 2     2 0 831 my ($exception_class, %args) = @_;
32              
33             # need to modify $Error::Depth (see Error.pm) to make certain parts
34             # of the call stack invisible to caller()
35             # +1 to make UNIVERSAL::throw() invisible
36             # in case it wasn't loaded; to make sure $Error::Depth isn't undef
37 2         20 require Error;
38 2         6 local $Error::Depth = $Error::Depth + 1;
39 2         232 eval "require $exception_class";
40 2 50       12 CORE::die($@) if $@;
41 2         25 $exception_class->throw(%args);
42             }
43              
44             # Similar reasoning for record().
45             sub UNIVERSAL::record {
46 0     0 0   my ($exception_class, %args) = @_;
47              
48             # need to modify $Error::Depth (see Error.pm) to make certain parts
49             # of the call stack invisible to caller()
50             # +1 to make UNIVERSAL::record() invisible
51             # in case it wasn't loaded; to make sure $Error::Depth isn't undef
52 0           require Error;
53 0           local $Error::Depth = $Error::Depth + 1;
54 0           eval "require $exception_class";
55 0 0         CORE::die $@ if $@;
56 0           $exception_class->record(%args);
57             }
58             1;
59              
60              
61             __END__