blib/lib/YATT/Lite/Partial/ErrorReporter.pm

filter: Statementstmt 95.2% 81 / 85 Branchbran 76.4% 26 / 34 Conditioncond 68.9% 20 / 29 MC/DCmcdc 50.0% 9 / 18 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 7 totaltotal 80.2% 150 / 187 CC 30 SCAR 20.9
CC
30
Cov
86%
raise
14.3
make_error
12.1
deref
4.0
CRAP 8.1
« blib/lib/YATT/Lite/CLI/Lint.pm ↑ Summary blib/lib/YATT/Lite/Util/AsBase.pm »
1 package YATT::Lite::Partial::ErrorReporter; sub MY () {__PACKAGE__}
2 # -*- coding: utf-8 -*-
3 81 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 81 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use YATT::Lite::Partial
6 81 ▶ (fields => [qw/at_done
Subroutine BEGIN: called CC=1 SCAR=0.0
7 error_handler
8 die_in_error
9 ext_pattern
10 in_sig_die
11 81 /]);
12 81 require Devel::StackTrace;
13  
14 81 ▶use constant DEBUG_ERROR => $ENV{DEBUG_YATT_ERROR};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 81 ▶use YATT::Lite::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 81 ▶use YATT::Lite::Util qw/incr_opt/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 #========================================
21 # error reporting.
22 #========================================
23  
24 136 ▶sub error {
Subroutine error: called CC=2 SCAR=7.1
Pod undocumented
25 136 ▶ (my MY $self) = map {ref $_ ? $_ : MY} shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 136 0 136
26 136 $self->raise(error => incr_opt(depth => \@_), @_);
27 }
28  
29 7 ▶sub error_with_status {
Subroutine error_with_status: called CC=2 SCAR=7.0
Pod undocumented
30 7 ▶ (my MY $self) = map {ref $_ ? $_ : MY} shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 7 0 7
31 7 my ($code) = shift;
32 7 my $opts = incr_opt(depth => \@_);
33 7 $opts->{http_status_code} = $code;
34 7 $self->raise(error => $opts, @_);
35 }
36  
37 218 ▶sub make_error {
Subroutine make_error: called CC=12 SCAR=24.9
Pod undocumented
38 218 my ($self, $depth, $opts) = splice @_, 0, 3;
39 218 my ($fmtOrReason, @args) = @_;
40 218 my ($pkg, $file, $line) = caller($depth);
41 218 my $bt = do {
42 218 my @bt_opts = (ignore_package => [__PACKAGE__]);
43 218 ▶ if (my $frm = delete $opts->{ignore_frame}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frm = delete $opts->{"ignore_frame"}) 24 194 218
44 # $YATT::Lite::CON->logdump(ignore_frame => $frm);
45 638 ▶ push @bt_opts, frame_filter => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
46 638 my ($hash) = @_;
47 638 my $caller = $hash->{'caller'};
48 638 ▶ my $all_match = grep {($frm->[$_] // '') eq ($caller->[$_] // '')}
Condition50%2/4
$frm->[$_] // ""
l!l
12760
$caller->[$_] // ""
l!l
12760
MC/DC0%0/1
$frm->[$_] // ""
$frm->[$_]
MC/DC0%0/1
$caller->[$_] // ""
$caller->[$_]
Truth table50%1/2
A
A= $frm->[$_]
Aresult
11
00
Truth table50%1/2
A
A= $caller->[$_]
Aresult
11
00
49 1, 2; # __FILE__, __LINE__
50 # print STDERR YATT::Lite::Util::terse_dump("filter: ", $all_match, $frm, $caller), "\n";
51 638 $all_match != 2;
52 }
53 24 }
54 218 Devel::StackTrace->new(@bt_opts);
55 };
56  
57 218 ▶ my $pattern = $self->{ext_pattern} // qr/\.(yatt|ytmpl|ydo)$/;
Condition50%1/2
$self->{"ext_pattern"} // qr/\.(yatt|ytmpl|ydo)$/
l!l
0218
MC/DC0%0/1
$self->{"ext_pattern"} // qr/\.(yatt|ytmpl|ydo)$/
$self->{"ext_pattern"}
Truth table50%1/2
A
A= $self->{"ext_pattern"}
Aresult
11
00
58  
59 218 my @tmplinfo;
60 218 foreach my $fr ($bt->frames) {
61 4412 ▶ my $fn = $fr->filename
Branch: 1 branch
BranchTrueFalseTotal
my $fn = $fr->filename or next 4412 0 4412
62 or next;
63 4412 ▶ $fn =~ $pattern
Branch: 1 branch
BranchTrueFalseTotal
$fn =~ /$pattern/ or next 44 4368 4412
64 or next;
65 44 push @tmplinfo, tmpl_file => $fn, tmpl_line => $fr->line;
66 44 last;
67 }
68  
69 218 my @error_diag = do {
70 218 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 181 37 218
71 # Errors from YATT can have arguments for sprintf.
72 181 (format => $fmtOrReason, args => \@args);
73 } else {
74 # (Possibly) errors from perl runtime itself. Should avoid use of sprintf.
75 37 (reason => do {
76 37 ▶ if (Encode::is_utf8($fmtOrReason) and not utf8::valid($fmtOrReason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($fmtOrReason) and not utf8::valid($fmtOrReason)) { } 1 36 37
Condition100%3/3
&Encode::is_utf8($fmtOrReason) and not utf8::valid($fmtOrReason)
!ll&&!rl&&r
3241
MC/DC100%2/2
&Encode::is_utf8($fmtOrReason) and not utf8::valid($fmtOrReason)
&Encode::is_utf8($fmtOrReason) not utf8::valid($fmtOrReason)
Truth table100%3/3
A and not(B)
A= &Encode::is_utf8($fmtOrReason)B= not utf8::valid($fmtOrReason)
ABresult
0X0
100
111
77 # Some errors from perl runtime could be trimmed to 32bytes and
78 # it can be malformed utf8.
79 1 YATT::Lite::Util::reencode_malformed_utf8($fmtOrReason);
80 } else {
81 36 $fmtOrReason;
82 }
83 });
84 }
85 };
86  
87 $self->Error->new
88 218 ▶ (file => $opts->{file} // $file, line => $opts->{line} // $line
Branch: 1 branch
BranchTrueFalseTotal
$opts ? : 218 0 218
Condition100%6/6
$opts->{"file"} // $file
l!l&&r!l&&!r
651512
$opts->{"line"} // $line
l!l&&r!l&&!r
651512
MC/DC100%2/2
$opts->{"file"} // $file
$opts->{"file"} $file
MC/DC100%2/2
$opts->{"line"} // $line
$opts->{"line"} $line
Truth table100%3/3
A // B
A= $opts->{"file"}B= $file
ABresult
1X1
011
000
Truth table100%3/3
A // B
A= $opts->{"line"}B= $line
ABresult
1X1
011
000
89 , @tmplinfo
90 , @error_diag
91 , backtrace => $bt
92 , $opts ? %$opts : ());
93 }
94  
95 # $yatt->raise($errType => ?{opts}?, $errFmt, @fmtArgs)
96  
97 218 ▶sub raise {
Subroutine raise: called CC=11 SCAR=26.6
Pod undocumented
98 218 (my MY $self, my $type) = splice @_, 0, 2;
99 218 ▶ my $opts = shift if @_ and ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ and ref $_[0] eq "HASH" 218 0 218
Condition33%1/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
00218
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
100 # shift/splice しないのは、引数を stack trace に残したいから
101 218 ▶ my $depth = (delete($opts->{depth}) // 0);
Condition50%1/2
delete $opts->{"depth"} // 0
l!l
2180
MC/DC0%0/1
delete $opts->{"depth"} // 0
delete $opts->{"depth"}
Truth table50%1/2
A
A= delete $opts->{"depth"}
Aresult
11
00
102 218 my Error $err = $self->make_error(2 + $depth, $opts, @_); # 2==raise+make_error
103 218 ▶ if (ref $self and my $sub = deref($self->{error_handler})) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $self and my $sub = deref($self->{'error_handler'})) { } 25 193 218
elsif ($sub = $self->can('error_handler')) { } 120 73 193
elsif (not ref $self or $self->{'die_in_error'}) { } 0 73 73
elsif ($err->{'http_status_code'}) { } 3 70 73
Condition66%2/3
ref $self and my $sub = deref($self->{"error_handler"})
!ll&&!rl&&r
019325
MC/DC50%1/2
ref $self and my $sub = deref($self->{"error_handler"})
ref $self my $sub = deref($self->{"error_handler"})
Truth table66%2/3
A and B
A= ref $selfB= my $sub = deref($self->{"error_handler"})
ABresult
0X0
100
111
104 # $con を引数で引きずり回すのは大変なので、むしろ外から closure を渡そう、と。
105 # $SIG{__DIE__} を使わないのはなぜかって? それはユーザに開放しておきたいのよん。
106 25 print STDERR "# raise by error_handler\n" if DEBUG_ERROR;
107 25 ▶ unless (ref $sub eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $sub eq "CODE") 0 25 25
108 0 die "error_handler is not a CODE ref: $sub";
109 }
110 25 $sub->($type, $err);
111 } elsif ($sub = $self->can('error_handler')) {
112 120 print STDERR "# raise by ->error_handler\n" if DEBUG_ERROR;
113 120 $sub->($self, $type, $err);
114 ▶ } elsif (not ref $self or $self->{die_in_error}) {
Condition33%1/3
not ref $self or $self->{"die_in_error"}
l!l&&r!l&&!r
0073
MC/DC0%0/2
not ref $self or $self->{"die_in_error"}
not ref $self $self->{"die_in_error"}
Truth table33%1/3
not(A) or B
A= not ref $selfB= $self->{"die_in_error"}
ABresult
1X1
011
000
115 0 print STDERR "# raise by die_in_error\n" if DEBUG_ERROR;
116 0 die $err->message;
117 } elsif ($err->{http_status_code}) {
118 3 print STDERR "# raise by http_status_code\n" if DEBUG_ERROR;
119 # If http_status_code is specified explicitly (from error_with_status),
120 # raise it immediately, with simple reason. (not full backtrace message).
121 $self->raise_psgi_html($err->{http_status_code}
122 3 , $err->reason);
123 } else {
124 70 print STDERR "# raise pass-thrue error object\n" if DEBUG_ERROR;
125 # 即座に die しないモードは、デバッガから error 呼び出し箇所に step して戻れるようにするため。
126 # ... でも、受け側を do {my $err = $con->error; die $err} にでもしなきゃダメかも?
127 70 return $err;
128 }
129 }
130  
131 # XXX: 将来、拡張されるかも。
132 83 ▶sub DONE {
Subroutine DONE: called CC=2 SCAR=7.6
Pod undocumented
133 83 my MY $self = shift;
134 83 ▶ if (my $sub = $self->{at_done}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->{'at_done'}) { } 0 83 83
135 0 $sub->(@_);
136 } else {
137 83 die \ 'DONE';
138 }
139 }
140  
141 3 ▶sub raise_psgi_html {
Subroutine raise_psgi_html: called CC=1 SCAR=0.0
Pod undocumented
142 3 (my MY $self, my ($status, $html, @rest)) = @_;
143 3 die [$status, ["Content-type" => "text/html; charset=utf-8", @rest]
144 , [$html]];
145 }
146  
147 218 ▶sub deref {
Subroutine deref: called CC=4 SCAR=13.9
Pod undocumented
148 218 ▶ return undef unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 73 145 218
149 145 ▶ if (ref $_[0] eq 'REF' or ref $_[0] eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'REF' or ref $_[0] eq 'SCALAR') { } 130 15 145
Condition100%3/3
ref $_[0] eq "REF" or ref $_[0] eq "SCALAR"
l!l&&r!l&&!r
1012015
MC/DC100%2/2
ref $_[0] eq "REF" or ref $_[0] eq "SCALAR"
ref $_[0] eq "REF" ref $_[0] eq "SCALAR"
Truth table100%3/3
A or B
A= ref $_[0] eq "REF"B= ref $_[0] eq "SCALAR"
ABresult
1X1
011
000
150 130 ${$_[0]};
151 } else {
152 15 $_[0];
153 }
154 }
155  
156 81 1;
« blib/lib/YATT/Lite/CLI/Lint.pm ↑ Summary blib/lib/YATT/Lite/Util/AsBase.pm »