Reporter/Sender/Dir.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 68.7% 11 / 16 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 13 / 13 Podpod 50.0% 1 / 2 totaltotal 84.3% 113 / 134 SCAR 28.0
CC
16
Cov
88%
send_report
11.1
new
6.1
__ANON__
2.0
CRAP 16.4
« Reporter.pm ↑ Summary Reporter/Dumper/YAML.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Exception::Reporter::Sender::Dir;
4 # ABSTRACT: a report sender that writes to directories on the filesystem
5 1 $Exception::Reporter::Sender::Dir::VERSION = '0.014';
6 1 ▶use parent 'Exception::Reporter::Sender';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 SYNOPSIS
9 #pod
10 #pod my $sender = Exception::Reporter::Sender::Dir->new({
11 #pod root => '/var/error/my-app',
12 #pod });
13 #pod
14 #pod =head1 OVERVIEW
15 #pod
16 #pod This report sender writes reports to the file system. Given a report with
17 #pod bunch dumpable items, the Dir sender will make a directory and write each item
18 #pod to a file in it, using the ident when practical, and a generated filename
19 #pod otherwise.
20 #pod
21 #pod =cut
22  
23 1 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use String::Truncate;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub new {
30 1 ▶ my ($class, $arg) = @_;
Subroutine new: called CC=5 SCAR=18.0
Pod undocumented
31  
32 1 ▶ my $root = $arg->{root} || Carp::confess("missing 'root' argument");
Condition33%1/3
$arg->{'root'} || Carp::confess(q[missing 'root' argument])
l!l&&r!l&&!r
100
MC/DC0%0/2
$arg->{'root'} || Carp::confess(q[missing 'root' argument])
$arg->{'root'} Carp::confess(q[missing 'root' argument])
Truth table33%1/3
A || B
A= $arg->{'root'}B= Carp::confess(q[missing 'root' argument])
ABresult
1X1
011
000
33  
34 1 ▶ use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 $root = path($root);
37  
38 1 ▶ if (-e $root && ! -d $root) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $root and not -d $root) 0 1 1
Condition33%1/3
-e $root and not -d $root
!ll&&!rl&&r
010
MC/DC0%0/2
-e $root and not -d $root
-e $root not -d $root
Truth table33%1/3
A and not(B)
A= -e $rootB= not -d $root
ABresult
0X0
100
111
39 0 Carp::confess("given root <$root> is not a writable directory");
40 }
41  
42 1 ▶ $root->mkpath unless -e $root;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $root 0 1 1
43  
44 1 return bless {
45 root => $root,
46 }, $class;
47 }
48  
49 #pod =head2 send_report
50 #pod
51 #pod $dir_reporter->send_report(\@summaries, \%arg, \%internal_arg);
52 #pod
53 #pod This method makes a subdirectory for the report and writes it out.
54 #pod
55 #pod C<%arg> is the same set of arguments given to Exception::Reporter's
56 #pod C<report_exception> method. Arguments that will have an effect include:
57 #pod
58 #pod reporter - the name of the program reporting the exception
59 #pod handled - if true, the reported exception was handled and the user
60 #pod saw a simple error message; adds C<< "handled":true >>
61 #pod to the JSON body of the report
62 #pod
63 #pod C<%internal_arg> contains data produced by the Exception::Reporter using this
64 #pod object. It includes the C<guid> of the report and the C<caller> calling the
65 #pod reporter.
66 #pod
67 #pod The return value of C<send_report> is not defined.
68 #pod
69 #pod =cut
70  
71 1 my $JSON;
72  
73 sub send_report {
74 1 ▶ my ($self, $summaries, $arg, $internal_arg) = @_;
Subroutine send_report: called CC=11 SCAR=24.1
Pod documented
75  
76 # ?!? Presumably this can't really happen, but... you know what they say
77 # about zero-summary incidents, right? -- rjbs, 2012-07-03
78 1 ▶ Carp::confess("can't report a zero-summary incident!") unless @$summaries;
Branch: 1 branch
BranchTrueFalseTotal
unless @$summaries 0 1 1
79  
80 # We always use this file for internal use.
81 1 my %manifest = ('report.json' => { description => 'report metadata' });
82 my %report = (
83 guid => $internal_arg->{guid},
84 1 manifest => \%manifest,
85 );
86  
87 1 my $n = 1;
88  
89 my $safename = sub {
90 9 ▶ my ($name) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
91 # Surely this is sub-optimal: -- rjbs, 2016-07-19
92 9 $name =~ s{\.\.}{DOTDOT}g;
93 9 $name =~ s{/}{BACKSLASH}g;
94 9 $name =~ s{[^-_0-9a-z.]}{-}gi;
95  
96 9 my $base = $name;
97 9 ▶ $name = "$base-" . $n++ while $manifest{$name};
Branch: 1 branch
BranchTrueFalseTotal
$manifest{$name} and do { $name = "$base-" . $n++ } 0 9 9
98  
99 9 return $name;
100 1 };
101  
102 1 my $root = $self->{root}->child($internal_arg->{guid});
103 1 $root->mkpath;
104  
105 1 my @parts;
106 1 GROUP: for my $summary (@$summaries) {
107 6 my @these_parts;
108  
109 6 my $t_path = \&path;
110 6 ▶ if (@{ $summary->[1] } > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$summary->[1];} > 1) 1 5 6
111 1 my $name = $safename->($summary->[0]);
112 1 $manifest{$name} = { ident => $summary->[0] };
113  
114 1 $root->child($name)->mkpath;
115 1 my $target_path = path($name);
116  
117 1 ▶ $t_path = sub { $target_path->child($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
118 }
119  
120 6 for my $inner (@{ $summary->[1] }) {
121 my $file = $t_path->(
122 8 ▶ $safename->( $inner->{filename} || 'inner' )
Condition50%1/2
$inner->{'filename'} || 'inner'
l!l
80
MC/DC0%0/1
$inner->{'filename'} || 'inner'
$inner->{'filename'}
Truth table50%1/2
A
A= $inner->{'filename'}
Aresult
11
00
123 );
124  
125 $manifest{$file} = {
126 filename => $inner->{filename},
127 content_type => $inner->{mimetype},
128  
129 (($inner->{body_is_bytes} && $inner->{charset})
130 ? (charset => $inner->{charset})
131 8 ▶ : ()),
Branch: 1 branch
BranchTrueFalseTotal
$inner->{'body_is_bytes'} && $inner->{'charset'} ? : 1 7 8
Condition100%3/3
$inner->{"body_is_bytes"} and $inner->{"charset"}
!ll&&!rl&&r
611
MC/DC100%2/2
$inner->{"body_is_bytes"} and $inner->{"charset"}
$inner->{"body_is_bytes"} $inner->{"charset"}
Truth table100%3/3
A and B
A= $inner->{"body_is_bytes"}B= $inner->{"charset"}
ABresult
0X0
100
111
132 };
133  
134 8 ▶ my $method = $inner->{body_is_bytes} ? 'spew_raw' : 'spew_utf8';
Branch: 1 branch
BranchTrueFalseTotal
$inner->{'body_is_bytes'} ? : 2 6 8
135 8 $root->child($file)->$method($inner->{body});
136 }
137 }
138  
139 1 ▶ if ($arg->{handled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"handled"}) 1 0 1
140 1 $report{handled} = \1;
141 }
142  
143 1 my ($package, $filename, $line) = @{ $internal_arg->{caller} };
144  
145 1 $report{reporter} = $arg->{reporter};
146 1 $report{caller} = "$filename line $line ($package)";
147  
148 1 ▶ $JSON ||= JSON->new->canonical->pretty;
Condition33%1/3
$JSON ||= "JSON"->new->canonical->pretty
l!l&&r!l&&!r
010
MC/DC0%0/2
$JSON ||= "JSON"->new->canonical->pretty
$JSON "JSON"->new->canonical->pretty
Truth table33%1/3
A ||= B
A= $JSONB= "JSON"->new->canonical->pretty
ABresult
1X1
011
000
149 1 my $json = $JSON->encode(\%report);
150  
151 1 $root->child('report.json')->spew_utf8($json);
152  
153 1 return;
154 }
155  
156 1 1;
157  
158 __END__
« Reporter.pm ↑ Summary Reporter/Dumper/YAML.pm »