File Coverage

lib/At/Error.pm
Criterion Covered Total %
statement 45 52 86.5
branch 6 8 75.0
condition 1 3 33.3
subroutine 12 13 92.3
pod 1 1 100.0
total 65 77 84.4


line stmt bran cond sub pod time code
1 8     8   232586 use v5.42;
  8         60  
2 8     8   54 use feature 'class';
  8         12  
  8         1515  
3 8     8   58 no warnings 'experimental::class';
  8         33  
  8         877  
4              
5             class At::Error 1.1 {
6 8     8   58 use Carp qw[];
  8         58  
  8         1009  
7             use overload
8 1     1   7 bool => sub {0},
9 8     8   54 '""' => sub ( $s, $u, $q ) { $s->message };
  8     0   13  
  8         103  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
10             field $message : param : reader;
11             field $description : param : reader //= ();
12             field $fatal : param : reader //= 0;
13             field @stack;
14             ADJUST {
15             my $i = 0;
16             while ( my $info = $self->_caller_info( ++$i ) ) {
17             push @stack, $info;
18             }
19             }
20              
21             method _caller_info($i) {
22             my ( $package, $filename, $line, $subroutine ) = caller($i);
23             return unless $package;
24             return { package => $package, file => $filename, line => $line, sub_name => $subroutine };
25             }
26              
27             method throw() {
28             my ( undef, $file, $line ) = caller();
29             my $msg = join "\n\t", sprintf( qq[%s at %s line %d\n], $message, $file, $line ),
30             map { sprintf q[%s called at %s line %d], $_->{sub_name}, $_->{file}, $_->{line} } @stack;
31             $fatal ? die "$msg\n" : warn "$msg\n";
32             }
33              
34             # Compatibility with old At::Error
35             sub import {
36 24     24   83 my $class = shift;
37 24         67 my $from = caller;
38 8     8   5615 no strict 'refs';
  8         19  
  8         3910  
39 24 100       100 my @syms = @_ ? @_ : qw[register throw];
40 24         58 for my $sym (@syms) {
41 48 100       139 if ( $sym eq 'register' ) {
    50          
42 24         53 *{ $from . '::register' } = \®ister;
  24         187  
43             }
44             elsif ( $sym eq 'throw' ) {
45 24         4463 *{ $from . '::throw' } = sub {
46 775     775   979 my $err = shift;
47 775 50 33     4624 if ( builtin::blessed($err) && $err->isa('At::Error') ) {
48 775         1932 $err->throw;
49             }
50             else {
51 0         0 die $err;
52             }
53 24         121 };
54             }
55             }
56             }
57              
58 35     35 1 344435 sub register( $name, $is_fatal = 0 ) {
  35         69  
  35         50  
  35         46  
59 35         90 my ($from) = caller;
60 8     8   61 no strict 'refs';
  8         15  
  8         1973  
61 35     775   203 *{ $from . '::' . $name } = sub ( $msg, $desc = '' ) {
  775         1173  
  775         1223  
  775         1173  
  775         989  
62 775         6458 At::Error->new( message => $msg, description => $desc, fatal => $is_fatal );
63 35         174 };
64             }
65             }
66             1;
67             __END__