Log/Report/Die.pm

filter: Statementstmt 82.6% 57 / 69 Branchbran 73.5% 25 / 34 Conditioncond 43.4% 10 / 23 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 70.4% 105 / 149 CC 30 SCAR 25.1
CC
30
Cov
73%
_exception_libxml
20.0
exception_decode
14.9
die_decode
13.0
CRAP 12.3
« Log/Report/Message.pm ↑ Summary Log/Report/Dispatcher/Syslog.pm »
1 # This code is part of Perl distribution Log-Report version 1.46.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2007-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Log::Report::Die;{
13 17 our $VERSION = '1.46';
14 }
15  
16 17 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 our @EXPORT = qw/die_decode exception_decode/;
22  
23 17 ▶use POSIX qw/locale_h/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #--------------------
26  
27 sub die_decode($%)
28 18 ▶{ my ($text, %args) = @_;
Subroutine die_decode: called CC=13 SCAR=25.7
Pod documented
29  
30 18 my @text = split /\n/, $text;
31 18 ▶ @text or return ();
Branch: 1 branch
BranchTrueFalseTotal
@text or return () 18 0 18
32 18 chomp $text[-1];
33  
34 # Try to catch the error directly, to remove it from the error text
35 18 my %opt = (errno => $! + 0);
36 18 my $err = "$!";
37  
38 18 ▶ if($text[0] =~ s/ at (.+) line (\d+)\.?$// )
Branch: 2 branches
BranchTrueFalseTotal
if ($text[0] =~ s/ at (.+) line (\d+)\.?$//) { } 11 7 18
elsif (@text > 1 and $text[1] =~ /^\s*at (.+) line (\d+)\.?$/) { } 4 5 9
39 11 { $opt{location} = [undef, $1, $2, undef];
40 }
41 ▶ elsif(@text > 1 && $text[1] =~ m/^\s*at (.+) line (\d+)\.?$/ )
Condition100%3/3
@text > 1 and $text[1] =~ /^\s*at (.+) line (\d+)\.?$/
!ll&&!rl&&r
214
MC/DC100%2/2
@text > 1 and $text[1] =~ /^\s*at (.+) line (\d+)\.?$/
@text > 1 $text[1] =~ /^\s*at (.+) line (\d+)\.?$/
Truth table100%3/3
A and B
A= @text > 1B= $text[1] =~ /^\s*at (.+) line (\d+)\.?$/
ABresult
0X0
100
111
42 { # sometimes people carp/confess with \n, folding the line
43 4 $opt{location} = [undef, $1, $2, undef];
44 4 splice @text, 1, 1;
45 }
46  
47 $text[0] =~ s/\s*[.:;]?\s*$err\s*$// # the $err is translation sensitive
48 18 ▶ or delete $opt{errno};
Branch: 1 branch
BranchTrueFalseTotal
$text[0] =~ s/\s*[.:;]?\s*$err\s*$// or delete $opt{"errno"} 9 9 18
49  
50 18 my @msg = shift @text;
51 18 ▶ length $msg[0] or $msg[0] = 'stopped';
Branch: 1 branch
BranchTrueFalseTotal
length $msg[0] or $msg[0] = "stopped" 18 0 18
52  
53 18 my @stack;
54 18 foreach (@text)
55 16 ▶ { if(m/^\s*(.*?)\s+called at (.*?) line (\d+)\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(.*?)\s+called at (.*?) line (\d+)\s*$/) { } 15 1 16
56 15 { push @stack, [ $1, $2, $3 ] }
57 1 else { push @msg, $_ }
58 }
59 18 $opt{stack} = \@stack;
60 18 ▶ $opt{classes} = [ 'perl', (@stack ? 'confess' : 'die') ];
Branch: 1 branch
BranchTrueFalseTotal
@stack ? : 5 13 18
61  
62 my $reason
63 = $opt{errno} ? 'FAULT'
64 : @stack ? 'PANIC'
65 18 ▶ : $args{on_die} || 'ERROR';
Branch: 2 branches
BranchTrueFalseTotal
$opt{'errno'} ? : 6 12 18
@stack ? : 3 9 12
Condition100%2/2
$args{"on_die"} or "ERROR"
l!l
54
MC/DC100%1/1
$args{"on_die"} or "ERROR"
$args{"on_die"}
Truth table100%2/2
A
A= $args{"on_die"}
Aresult
11
00
66  
67 18 (\%opt, $reason, join("\n", @msg), 'die');
68 }
69  
70  
71 sub _exception_dbix($$)
72 5 ▶{ my ($exception, $args) = @_;
Subroutine _exception_dbix: called CC=11 SCAR=24.6
73 5 my $on_die = delete $args->{on_die};
74 5 my %opts = %$args;
75  
76 5 my @lines = split /\n/, "$exception"; # accessor missing to get msg
77 5 my $first = shift @lines;
78 5 my ($sub, $message, $fn, $linenr) = $first =~
79 m/^ (?: ([\w:]+?) \(\)\: [ ] | \{UNKNOWN\}\: [ ] )?
80 (.*?)
81 \s+ at [ ] (.+) [ ] line [ ] ([0-9]+)\.?
82 $/x;
83 5 ▶ my $pkg = defined $sub && $sub =~ s/^([\w:]+)\:\:// ? $1 : $0;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub && $sub =~ s/^([\w:]+)\:\:// ? : 2 3 5
Condition66%2/3
defined $sub and $sub =~ s/^([\w:]+)\:\://
!ll&&!rl&&r
302
MC/DC50%1/2
defined $sub and $sub =~ s/^([\w:]+)\:\://
defined $sub $sub =~ s/^([\w:]+)\:\://
Truth table66%2/3
A and B
A= defined $subB= $sub =~ s/^([\w:]+)\:\://
ABresult
0X0
100
111
84  
85 5 ▶ $opts{location} ||= [ $pkg, $fn, $linenr, $sub ];
Condition50%1/2
$opts{"location"} ||= [$pkg, $fn, $linenr, $sub]
l!l
05
MC/DC0%0/1
$opts{"location"} ||= [$pkg, $fn, $linenr, $sub]
$opts{"location"}
Truth table50%1/2
A
A= $opts{"location"}
Aresult
11
00
86  
87 5 my @stack;
88 5 foreach (@lines)
89 2 ▶ { my ($func, $fn, $linenr) = /^\s+(.*?)\(\)\s+called at (.*?) line ([0-9]+)$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = /^\s+(.*?)\(\)\s+called at (.*?) line ([0-9]+)$/ or next 2 0 2
90 2 push @stack, [ $func, $fn, $linenr ];
91 }
92 5 ▶ $opts{stack} ||= \@stack if @stack;
Branch: 1 branch
BranchTrueFalseTotal
if @stack 1 4 5
Condition50%1/2
$opts{"stack"} ||= \@stack
l!l
01
MC/DC0%0/1
$opts{"stack"} ||= \@stack
$opts{"stack"}
Truth table50%1/2
A
A= $opts{"stack"}
Aresult
11
00
93  
94 my $reason
95 5 ▶ = $opts{errno} ? 'FAULT'
Branch: 2 branches
BranchTrueFalseTotal
$opts{'errno'} ? : 0 5 5
@stack ? : 1 4 5
Condition50%1/2
$on_die or "ERROR"
l!l
04
MC/DC0%0/1
$on_die or "ERROR"
$on_die
Truth table50%1/2
A
A= $on_die
Aresult
11
00
96 : @stack ? 'PANIC'
97 : $on_die || 'ERROR';
98  
99 5 (\%opts, $reason, $message, 'exception, dbix');
100 }
101  
102 17 my %_libxml_errno2reason = (1 => 'WARNING', 2 => 'MISTAKE', 3 => 'ERROR');
103  
104 sub _exception_libxml($$)
105 0 ▶{ my ($exc, $args) = @_;
Subroutine _exception_libxml: not called CC=4 SCAR=30.0
106 0 my $on_die = delete $args->{on_die};
107 0 my %opts = %$args;
108  
109 0 ▶ $opts{errno} ||= $exc->code + 13000;
Condition0%0/3
$opts{"errno"} ||= $exc->code + 13000
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"errno"} ||= $exc->code + 13000
$opts{"errno"} $exc->code + 13000
Truth table0%0/3
A ||= B
A= $opts{"errno"}B= $exc->code + 13000
ABresult
1X1
011
000
110 0 ▶ $opts{location} ||= [ 'libxml', $exc->file, $exc->line, $exc->domain ];
Condition0%0/2
$opts{"location"} ||= ["libxml", $exc->file, $exc->line, $exc->domain]
l!l
00
MC/DC0%0/1
$opts{"location"} ||= ["libxml", $exc->file, $exc->line, $exc->domain]
$opts{"location"}
Truth table0%0/2
A
A= $opts{"location"}
Aresult
11
00
111  
112 0 my $msg = $exc->message . $exc->context . "\n"
113 . (' ' x $exc->column) . '^'
114 . ' (' . $exc->domain . ' error ' . $exc->code . ')';
115  
116 0 ▶ my $reason = $_libxml_errno2reason{$exc->level} || 'PANIC';
Condition0%0/2
$_libxml_errno2reason{$exc->level} || 'PANIC'
l!l
00
MC/DC0%0/1
$_libxml_errno2reason{$exc->level} || 'PANIC'
$_libxml_errno2reason{$exc->level}
Truth table0%0/2
A
A= $_libxml_errno2reason{$exc->level}
Aresult
11
00
117 0 (\%opts, $reason, $msg, 'exception, libxml');
118 }
119  
120 sub exception_decode($%)
121 5 ▶{ my ($exception, %args) = @_;
Subroutine exception_decode: called CC=5 SCAR=27.0
Pod documented
122 5 my $errno = $! + 0;
123  
124 5 ▶ return _exception_dbix($exception, \%args)
Branch: 1 branch
BranchTrueFalseTotal
if $exception->isa("DBIx::Class::Exception") 5 0 5
125 if $exception->isa('DBIx::Class::Exception');
126  
127 0 ▶ return _exception_libxml($exception, \%args)
Branch: 1 branch
BranchTrueFalseTotal
if $exception->isa("XML::LibXML::Error") 0 0 0
128 if $exception->isa('XML::LibXML::Error');
129  
130 # Unsupported exception system, sane guesses
131 0 my %opt = (
132 classes => [ 'unknown exception', 'die', ref $exception ],
133 errno => $errno,
134 );
135  
136 0 ▶ my $reason = $errno ? 'FAULT' : ($args{on_die} || 'ERROR');
Branch: 1 branch
BranchTrueFalseTotal
$errno ? : 0 0 0
Condition0%0/2
$args{"on_die"} or "ERROR"
l!l
00
MC/DC0%0/1
$args{"on_die"} or "ERROR"
$args{"on_die"}
Truth table0%0/2
A
A= $args{"on_die"}
Aresult
11
00
137  
138 # hopefully stringification is overloaded
139 0 (\%opt, $reason, "$exception", 'exception');
140 }
141  
142 17 "to die or not to die, that's the question";
« Log/Report/Message.pm ↑ Summary Log/Report/Dispatcher/Syslog.pm »