File Coverage

blib/lib/Error/TypeTiny.pm
Criterion Covered Total %
statement 52 53 98.1
branch 13 18 72.2
condition 8 14 57.1
subroutine 16 16 100.0
pod 8 8 100.0
total 97 109 88.9


line stmt bran cond sub pod time code
1             package Error::TypeTiny;
2              
3 297     297   17509 use 5.008001;
  297         1297  
4 297     297   1733 use strict;
  297         645  
  297         10015  
5 297     297   2543 use warnings;
  297         979  
  297         23228  
6              
7             BEGIN {
8 297     297   1199 $Error::TypeTiny::AUTHORITY = 'cpan:TOBYINK';
9 297         192713 $Error::TypeTiny::VERSION = '2.010001';
10             }
11              
12             $Error::TypeTiny::VERSION =~ tr/_//d;
13              
14             require Type::Tiny;
15             __PACKAGE__->Type::Tiny::_install_overloads(
16 750     750   323527 q[""] => sub { local $@; $_[0]->to_string },
  750         4123  
17 1704     1704   19725 q[bool] => sub { 1 },
18             );
19              
20             require Carp;
21             *CarpInternal = \%Carp::CarpInternal;
22              
23             our %CarpInternal;
24             $CarpInternal{$_}++ for @Type::Tiny::InternalPackages;
25              
26             sub new {
27 904     904 1 1601 my $class = shift;
28 904 50       6181 my %params = ( @_ == 1 ) ? %{ $_[0] } : @_;
  0         0  
29 904         4391 return bless \%params, $class;
30             }
31              
32             sub throw {
33 463     463 1 64497 my $next = $_[0]->can( 'throw_cb' );
34 463         1733 splice( @_, 1, 0, undef );
35 463         2746 goto $next;
36             }
37              
38             sub throw_cb {
39 903     903 1 2976 my $class = shift;
40 903         1549 my $callback = shift;
41            
42 903         2283 my ( $level, @caller, %ctxt ) = 0;
43 903         2283 while (
44             do {
45 2580         5490 my $caller = caller $level;
46 2580 50       11509 defined $caller and $CarpInternal{$caller};
47             }
48             )
49             {
50 1677         2614 $level++;
51             }
52 903 100 100     11402 if ( ( ( caller( $level - 1 ) )[1] || "" ) =~
53             /^(?:parameter validation for|exportable function) '(.+?)'$/ )
54             {
55 677         5252 my ( $pkg, $func ) = ( $1 =~ m{^(.+)::(\w+)$} );
56 677 100 50     3154 $level++ if caller( $level ) eq ( $pkg || "" );
57             }
58            
59             {
60 297     297   2596 no warnings 'uninitialized';
  297         1288  
  297         167638  
  903         2003  
61             # Moo's Method::Generate::Constructor puts an eval in the stack trace,
62             # that is useless for debugging, so show the stack frame one above.
63 903 50 33     5547 $level++
64             if (
65             ( caller( $level ) )[1] =~ /^\(eval \d+\)$/
66             and ( caller( $level ) )[3] eq '(eval)' # (caller())[3] is $subroutine
67             );
68 903         6697 @ctxt{qw/ package file line /} = caller( $level );
69             }
70            
71 903         2203 my $stack = undef;
72 903 100       2312 if ( our $StackTrace ) {
73 1         6 require Devel::StackTrace;
74 1         29 $stack = "Devel::StackTrace"->new(
75             ignore_package => [ keys %CarpInternal ],
76             );
77             }
78            
79 903         4821 our $LastError = $class->new(
80             context => \%ctxt,
81             stack_trace => $stack,
82             @_,
83             );
84            
85 903 100       10881 $callback ? $callback->( $LastError ) : die( $LastError );
86             } #/ sub throw
87              
88 770   66 770 1 11170 sub message { $_[0]{message} ||= $_[0]->_build_message }
89 757     757 1 3623 sub context { $_[0]{context} }
90 3     3 1 3702 sub stack_trace { $_[0]{stack_trace} }
91              
92             sub to_string {
93 402     402 1 953 my $e = shift;
94 402         1364 my $c = $e->context;
95 402         1047 my $m = $e->message;
96              
97             $m =~ /\n\z/s
98             ? $m
99             : $c ? sprintf(
100             "%s at %s line %s.\n", $m, $c->{file} || 'file?',
101 402 50 50     5893 $c->{line} || 'NaN'
    50 50        
102             )
103             : sprintf( "%s\n", $m );
104             } #/ sub to_string
105              
106             sub _build_message {
107 1     1   10 return 'An exception has occurred';
108             }
109              
110             sub croak {
111 141     141 1 432 my ( $fmt, @args ) = @_;
112 141         848 @_ = (
113             __PACKAGE__,
114             message => sprintf( $fmt, @args ),
115             );
116 141         569 goto \&throw;
117             }
118              
119             1;
120              
121             __END__