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

filter: Statementstmt 95.2% 81 / 85 Branchbran 76.4% 26 / 34 Conditioncond 66.6% 20 / 30 MC/DCmcdc 47.3% 9 / 19 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 7 totaltotal 79.3% 150 / 189 CC 30 SCAR 20.9
CC
30
Cov
85%
raise
14.3
make_error
12.2
deref
4.0
CRAP 8.1
« blib/lib/YATT/Lite/WebMVC0/DBSchema.pm ↑ Summary blib/lib/YATT/Lite/Util/AsBase.pm »
1 package YATT::Lite::Partial::ErrorReporter; sub MY () {__PACKAGE__}
2 # -*- coding: utf-8 -*-
3 53 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 53 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use YATT::Lite::Partial
6 53 ▶ (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 53 /]);
12 53 require Devel::StackTrace;
13  
14 53 ▶use constant DEBUG_ERROR => $ENV{DEBUG_YATT_ERROR};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 53 ▶use YATT::Lite::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 53 ▶use YATT::Lite::Util qw/incr_opt/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 #========================================
21 # error reporting.
22 #========================================
23  
24 73 ▶sub error {
Subroutine error: called CC=2 SCAR=7.1
Pod undocumented
25 73 ▶ (my MY $self) = map {ref $_ ? $_ : MY} shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 73 0 73
26 73 $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 85 ▶sub make_error {
Subroutine make_error: called CC=12 SCAR=25.0
Pod undocumented
38 85 my ($self, $depth, $opts) = splice @_, 0, 3;
39 85 my ($fmtOrReason, @args) = @_;
40 85 my ($pkg, $file, $line) = caller($depth);
41 85 my $bt = do {
42 85 my @bt_opts = (ignore_package => [__PACKAGE__]);
43 85 ▶ if (my $frm = delete $opts->{ignore_frame}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frm = delete $opts->{"ignore_frame"}) 9 76 85
44 # $YATT::Lite::CON->logdump(ignore_frame => $frm);
45 234 ▶ push @bt_opts, frame_filter => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
46 234 my ($hash) = @_;
47 234 my $caller = $hash->{'caller'};
48 234 ▶ my $all_match = grep {($frm->[$_] // '') eq ($caller->[$_] // '')}
Condition50%2/4
$frm->[$_] // ""
l!l
4680
$caller->[$_] // ""
l!l
4680
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 234 $all_match != 2;
52 }
53 9 }
54 85 Devel::StackTrace->new(@bt_opts);
55 };
56  
57 85 ▶ my $pattern = $self->{ext_pattern} // qr/\.(yatt|ytmpl|ydo)$/;
Condition33%1/3
$self->{"ext_pattern"} // qr/\.(yatt|ytmpl|ydo)$/
l!l&&r!l&&!r
0850
MC/DC0%0/2
$self->{"ext_pattern"} // qr/\.(yatt|ytmpl|ydo)$/
$self->{"ext_pattern"} qr/\.(yatt|ytmpl|ydo)$/
Truth table33%1/3
A // B
A= $self->{"ext_pattern"}B= qr/\.(yatt|ytmpl|ydo)$/
ABresult
1X1
011
000
58  
59 85 my @tmplinfo;
60 85 foreach my $fr ($bt->frames) {
61 1820 ▶ my $fn = $fr->filename
Branch: 1 branch
BranchTrueFalseTotal
my $fn = $fr->filename or next 1820 0 1820
62 or next;
63 1820 ▶ $fn =~ $pattern
Branch: 1 branch
BranchTrueFalseTotal
$fn =~ /$pattern/ or next 4 1816 1820
64 or next;
65 4 push @tmplinfo, tmpl_file => $fn, tmpl_line => $fr->line;
66 4 last;
67 }
68  
69 85 my @error_diag = do {
70 85 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 66 19 85
71 # Errors from YATT can have arguments for sprintf.
72 66 (format => $fmtOrReason, args => \@args);
73 } else {
74 # (Possibly) errors from perl runtime itself. Should avoid use of sprintf.
75 19 (reason => do {
76 19 ▶ 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 18 19
Condition100%3/3
&Encode::is_utf8($fmtOrReason) and not utf8::valid($fmtOrReason)
!ll&&!rl&&r
1531
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 18 $fmtOrReason;
82 }
83 });
84 }
85 };
86  
87 $self->Error->new
88 85 ▶ (file => $opts->{file} // $file, line => $opts->{line} // $line
Branch: 1 branch
BranchTrueFalseTotal
$opts ? : 85 0 85
Condition100%6/6
$opts->{"file"} // $file
l!l&&r!l&&!r
35482
$opts->{"line"} // $line
l!l&&r!l&&!r
35482
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 85 ▶sub raise {
Subroutine raise: called CC=11 SCAR=26.6
Pod undocumented
98 85 (my MY $self, my $type) = splice @_, 0, 2;
99 85 ▶ my $opts = shift if @_ and ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ and ref $_[0] eq "HASH" 85 0 85
Condition33%1/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
0085
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 85 ▶ my $depth = (delete($opts->{depth}) // 0);
Condition50%1/2
delete $opts->{"depth"} // 0
l!l
850
MC/DC0%0/1
delete $opts->{"depth"} // 0
delete $opts->{"depth"}
Truth table50%1/2
A
A= delete $opts->{"depth"}
Aresult
11
00
102 85 my Error $err = $self->make_error(2 + $depth, $opts, @_); # 2==raise+make_error
103 85 ▶ if (ref $self and my $sub = deref($self->{error_handler})) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $self and my $sub = deref($self->{'error_handler'})) { } 11 74 85
elsif ($sub = $self->can('error_handler')) { } 22 52 74
elsif (not ref $self or $self->{'die_in_error'}) { } 0 52 52
elsif ($err->{'http_status_code'}) { } 3 49 52
Condition66%2/3
ref $self and my $sub = deref($self->{"error_handler"})
!ll&&!rl&&r
07411
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 11 print STDERR "# raise by cf_error_handler\n" if DEBUG_ERROR;
107 11 ▶ unless (ref $sub eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $sub eq "CODE") 0 11 11
108 0 die "error_handler is not a CODE ref: $sub";
109 }
110 11 $sub->($type, $err);
111 } elsif ($sub = $self->can('error_handler')) {
112 22 print STDERR "# raise by ->error_handler\n" if DEBUG_ERROR;
113 22 $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
0052
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 49 print STDERR "# raise pass-thrue error object\n" if DEBUG_ERROR;
125 # 即座に die しないモードは、デバッガから error 呼び出し箇所に step して戻れるようにするため。
126 # ... でも、受け側を do {my $err = $con->error; die $err} にでもしなきゃダメかも?
127 49 return $err;
128 }
129 }
130  
131 # XXX: 将来、拡張されるかも。
132 13 ▶sub DONE {
Subroutine DONE: called CC=2 SCAR=7.6
Pod undocumented
133 13 my MY $self = shift;
134 13 ▶ if (my $sub = $self->{at_done}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->{'at_done'}) { } 0 13 13
135 0 $sub->(@_);
136 } else {
137 13 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 85 ▶sub deref {
Subroutine deref: called CC=4 SCAR=13.9
Pod undocumented
148 85 ▶ return undef unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 52 33 85
149 33 ▶ if (ref $_[0] eq 'REF' or ref $_[0] eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'REF' or ref $_[0] eq 'SCALAR') { } 31 2 33
Condition100%3/3
ref $_[0] eq "REF" or ref $_[0] eq "SCALAR"
l!l&&r!l&&!r
9222
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 31 ${$_[0]};
151 } else {
152 2 $_[0];
153 }
154 }
155  
156 53 1;
« blib/lib/YATT/Lite/WebMVC0/DBSchema.pm ↑ Summary blib/lib/YATT/Lite/Util/AsBase.pm »