| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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->[$_] // ""
$caller->[$_] // ""
MC/DC0%0/1
$frm->[$_] // ""
$frm->[$_]
MC/DC0%0/1
$caller->[$_] // ""
$caller->[$_]
Truth table50%1/2
A
A= $frm->[$_]
Truth table50%1/2
A
A= $caller->[$_]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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)$/
MC/DC0%0/1
$self->{"ext_pattern"} // qr/\.(yatt|ytmpl|ydo)$/
$self->{"ext_pattern"}
Truth table50%1/2
A
A= $self->{"ext_pattern"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 218 | my @tmplinfo; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 218 | foreach my $fr ($bt->frames) { | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 4412 | ▶ | my $fn = $fr->filename | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | or next; | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 4412 | ▶ | $fn =~ $pattern | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
&Encode::is_utf8($fmtOrReason) and not utf8::valid($fmtOrReason)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$opts->{"file"} // $file
$opts->{"line"} // $line
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
Truth table100%3/3
A // B
A= $opts->{"line"}B= $line
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
@_ and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # shift/splice しないのは、引数を stack trace に残したいから | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 218 | ▶ | my $depth = (delete($opts->{depth}) // 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts->{"depth"} // 0
MC/DC0%0/1
delete $opts->{"depth"} // 0
delete $opts->{"depth"}
Truth table50%1/2
A
A= delete $opts->{"depth"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $self and my $sub = deref($self->{"error_handler"})
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"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 145 | ▶ | if (ref $_[0] eq 'REF' or ref $_[0] eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $_[0] eq "REF" or ref $_[0] eq "SCALAR"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 130 | ${$_[0]}; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 15 | $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 81 | 1; | |||||||||||||||||||||||||||||||||||||||||||||