| 1 | package Unexpected::TraitFor::Throwing; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp ( ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Unexpected::Functions qw( is_one_of_us parse_arg_list ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Unexpected::Types qw( Maybe Object ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | requires qw( BUILD ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Private functions | ||||||||||||||||||||||||||||
| 15 | 61 | ▶ | my $_cache_key = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 16 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 17 | 61 | ▶ | return $PID.'-'.(exists $INC{ 'threads.pm' } ? threads->tid() : 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 4 | }; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | my $_exception_cache = {}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Public attributes. Lifted from Throwable | ||||||||||||||||||||||||||||
| 23 | has 'previous_exception' => is => 'ro', isa => Maybe[Object], | ||||||||||||||||||||||||||||
| 24 | 26 | ▶ | builder => sub { $_exception_cache->{ $_cache_key->() } }; | ||||||||||||||||||||||||||
|
Subroutine
_build_previous_exception: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Construction | ||||||||||||||||||||||||||||
| 27 | 31 | ▶ | after 'BUILD' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 31 | my $self = shift; my $e = $self->clone; delete $e->{previous_exception}; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 31 | $_exception_cache->{ $_cache_key->() } = $e; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 31 | return; | |||||||||||||||||||||||||||
| 33 | 4 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Private methods | ||||||||||||||||||||||||||||
| 36 | 40 | ▶ | my $_is_object_ref = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 37 | 40 | ▶ | my ($self, @args) = @_; blessed $self or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 3 | ▶ | scalar @args and Carp::confess | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 'Trying to throw an Exception object with arguments'; | ||||||||||||||||||||||||||||
| 41 | 2 | return 1; | |||||||||||||||||||||||||||
| 42 | 4 | }; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Public methods | ||||||||||||||||||||||||||||
| 45 | 10 | ▶ | sub caught { | ||||||||||||||||||||||||||
|
Subroutine
caught: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 10 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 10 | ▶ | $self->$_is_object_ref( @args ) and return $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 9 | my $attr = parse_arg_list @args; | |||||||||||||||||||||||||||
| 51 | 9 | ▶ | my $error = $attr->{error} ||= $EVAL_ERROR; $error or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$attr->{"error"} ||= $Unexpected::TraitFor::Throwing::EVAL_ERROR
MC/DC100%2/2
$attr->{"error"} ||= $Unexpected::TraitFor::Throwing::EVAL_ERROR
$attr->{"error"}
$Unexpected::TraitFor::Throwing::EVAL_ERROR
Truth table100%3/3
A ||= B
A= $attr->{"error"}B= $Unexpected::TraitFor::Throwing::EVAL_ERROR
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 8 | ▶ | return (is_one_of_us $error) ? $error : $self->new( $attr ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 33 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 33 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 33 | ▶ | my $class = blessed $self or $self->throw( 'Clone is an object method' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 32 | ▶ | return bless { %{ $self }, %{ $args // {} } }, $class; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args // {}
MC/DC100%1/1
$args // {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 30 | ▶ | sub throw { | ||||||||||||||||||||||||||
|
Subroutine
throw: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 30 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 30 | ▶ | $self->$_is_object_ref( @args ) and die $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 28 | ▶ | is_one_of_us $args[ 0 ] and die $args[ 0 ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 27 | die $self->new( @args ); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | ▶ | sub throw_on_error { | ||||||||||||||||||||||||||
|
Subroutine
throw_on_error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | my $e; $e = shift->caught( @_ ) and die $e; return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | 1; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||