| 1 | package Exception::Chain; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.008005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | use overload | ||||||||||||||||||||||||||||
| 6 | 3 | ▶ | '""' => sub { $_[0]->to_string || $_[0] }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | use Class::Accessor::Lite ( | ||||||||||||||||||||||||||||
| 9 | 3 | ro => [qw/ delivery is_delivery_duplicated duplicated_trace /], | |||||||||||||||||||||||||||
| 10 | 3 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Time::Piece qw(localtime); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Time::HiRes qw(gettimeofday tv_interval); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Data::Util qw(is_instance); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | our $VERSION = "0.10"; | |||||||||||||||||||||||||||
| 17 | 3 | our $SkipDepth = 0; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # class method | ||||||||||||||||||||||||||||
| 20 | sub new { | ||||||||||||||||||||||||||||
| 21 | 2 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | my $self = bless { | |||||||||||||||||||||||||||
| 24 | tags => {}, | ||||||||||||||||||||||||||||
| 25 | stack => [], | ||||||||||||||||||||||||||||
| 26 | message => undef, | ||||||||||||||||||||||||||||
| 27 | delivery => undef, | ||||||||||||||||||||||||||||
| 28 | is_delivery_duplicated => 0, | ||||||||||||||||||||||||||||
| 29 | duplicated_trace => [], | ||||||||||||||||||||||||||||
| 30 | }, $class; | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub _get_external_caller { | ||||||||||||||||||||||||||||
| 34 | 3 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
_get_external_caller: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 35 | 3 | my $i = 0; | |||||||||||||||||||||||||||
| 36 | 3 | ▶ | while (my @caller = caller(++$i)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 6 | ▶ | unless ($caller[0] =~ /^Exception::Chain/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 3 | my $level = $i + $SkipDepth; | |||||||||||||||||||||||||||
| 39 | 3 | @caller = caller($level); | |||||||||||||||||||||||||||
| 40 | 3 | return @caller; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub _build_arg { | ||||||||||||||||||||||||||||
| 46 | 3 | ▶ | my ($class, @info) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_arg: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | if (scalar @info == 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 0 | return { }; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | elsif (scalar @info == 1) { | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | if ($class->_is_my_instance($info[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | return { error => $info[0] }; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||
| 56 | 0 | return { message => $class->dumper($info[0]) }; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 2 | my %data = @info; | |||||||||||||||||||||||||||
| 61 | 2 | my $ret = {}; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | $ret->{message} = $class->dumper($data{message}); | |||||||||||||||||||||||||||
| 64 | 2 | for my $name (qw/tag error delivery/) { | |||||||||||||||||||||||||||
| 65 | 6 | $ret->{$name} = $data{$name}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | return $ret; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub _is_my_instance { | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | my ($class, $instance) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_is_my_instance: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 2 | is_instance($instance, 'Exception::Chain'); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub dumper { | ||||||||||||||||||||||||||||
| 78 | 2 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dumper: called CC=3 SCAR=15.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | if ( defined $value && ref($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and ref $value
MC/DC0%0/2
defined $value and ref $value
defined $value
ref $value
Truth table33%1/3
A and B
A= defined $valueB= ref $value
| |||||||||||||||||||||||||||||
| 80 | 0 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 81 | 0 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||
| 82 | 0 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 83 | 0 | return Data::Dumper::Dumper($value); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 2 | return $value; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # instance method | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub throw { | ||||||||||||||||||||||||||||
| 91 | 3 | ▶ | my ($class, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
throw: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 3 | my $builded_args = $class->_build_arg(@args); | |||||||||||||||||||||||||||
| 93 | 3 | my $self; | |||||||||||||||||||||||||||
| 94 | 3 | ▶ | if (not defined $builded_args->{error}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 95 | 2 | $self = $class->new; | |||||||||||||||||||||||||||
| 96 | 2 | $self->{message} = $builded_args->{message}; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | elsif ($class->_is_my_instance($builded_args->{error})) { | ||||||||||||||||||||||||||||
| 99 | 1 | $self = delete $builded_args->{error}; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 0 | $self = $class->new; | |||||||||||||||||||||||||||
| 103 | 0 | push @{$self->{stack}}, $builded_args->{error}; | |||||||||||||||||||||||||||
| 104 | 0 | $self->rethrow(%{$builded_args}); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 3 | $self->logging($builded_args); | |||||||||||||||||||||||||||
| 107 | 3 | die $self; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub rethrow { | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rethrow: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | $self->logging($self->_build_arg(@args)); | |||||||||||||||||||||||||||
| 113 | 0 | die $self; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub to_string { | ||||||||||||||||||||||||||||
| 117 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 6 | my $string = join( ' ', @{$self->{stack}} ); | |||||||||||||||||||||||||||
| 119 | 6 | $string =~ s/\n//g; | |||||||||||||||||||||||||||
| 120 | 6 | return $string; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub match { | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my ($self, @tags) = @_; | ||||||||||||||||||||||||||
|
Subroutine
match: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 0 | return scalar grep { defined $self->{tags}{$_} } @tags; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub first_message { | ||||||||||||||||||||||||||||
| 129 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
first_message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 1 | return $self->{message}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub add_message { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my ($self, $message) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_message: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 0 | $self->logging($self->_build_arg($message)); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub logging { | ||||||||||||||||||||||||||||
| 139 | 3 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
logging: called CC=9 SCAR=31.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 3 | my ($pkg, $file, $line) = $self->_get_external_caller; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 3 | ▶ | if (%{$args}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 2 | ▶ | if (my $tags = $args->{tag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $tags = [$tags] unless ref $tags; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | for my $tag (@$tags) { | |||||||||||||||||||||||||||
| 147 | 0 | $self->{tags}{$tag} = 1; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 2 | ▶ | if (my $message = $args->{message}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 2 | push @{$self->{stack}}, "$message at $file line $line."; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 2 | ▶ | if (my $delivery = $args->{delivery}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($self->delivery) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | unless ($self->is_delivery_duplicated) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | $self->{is_delivery_duplicated} = 1; | |||||||||||||||||||||||||||
| 157 | 0 | push @{$self->{duplicated_trace}}, $self->{stack}->[0]; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 0 | push @{$self->{duplicated_trace}}, "$file line $line." ; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||
| 162 | 0 | $self->{delivery} = $delivery; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||
| 167 | 1 | push @{$self->{stack}}, "at $file line $line."; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3 | 1; | |||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||