blib/lib/ASP4/Error.pm

filter: Statementstmt 18.0% 13 / 72 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 21.0% 4 / 19 Podpod 92.8% 13 / 14 totaltotal 20.4% 30 / 147 CC 19 SCAR 49.6
CC
19
Cov
12%
new
306.0
_number_range
12.0
domain
2.0
CRAP 142.4
« blib/lib/ASP4/ModPerl.pm ↑ Summary blib/lib/ASP4/ConfigFinder.pm »
1  
2 package ASP4::Error;
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use ASP4::HTTPContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 sub new
11 0 ▶{
Subroutine new: not called CC=17 SCAR=57.2
Pod undocumented
12 0 my $class = shift;
13 0 my ($err_str, %args);
14 0 ▶ if( @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif ($@) { } 0 0 0
15 {
16 0 ▶ if( @_ == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
17 {
18 0 $err_str = shift;
19 }
20 else
21 {
22 0 %args = @_;
23 }# end if()
24 }
25 elsif( $@ )
26 {
27 0 $err_str = $@;
28 }# end if()
29
30 0 my $context = ASP4::HTTPContext->current;
31 0 my $Config = $context->config;
32 0 my $Response = $context->response;
33 0 my $Session = $context->session;
34 0 my $Form = $context->request->Form;
35
36 0 my %session_data = %$Session;
37
38 0 my $error;
39 0 ▶ if( $err_str )
Branch: 1 branch
BranchTrueFalseTotal
if ($err_str) { } 0 0 0
40 {
41 0 my ($main, $message, $file, $line) = $err_str =~ m/^((.*?)\s(?:at|in)\s(.*?)\sline\s(\d+))/;
42 0 $error = {
43 message => $err_str,
44 file => $file,
45 line => $line,
46 stacktrace => $err_str,
47 };
48
49 0 ▶ if( $error->{file} =~ m{_asp\.pm$} )
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"file"} =~ /_asp\.pm$/) 0 0 0
50 {
51 0 ▶ $error->{file} = -f $ENV{SCRIPT_FILENAME} ? $ENV{SCRIPT_FILENAME} : $error->{file};
Branch: 1 branch
BranchTrueFalseTotal
-f $ENV{'SCRIPT_FILENAME'} ? : 0 0 0
52 }# end if()
53 }
54 else
55 {
56 0 $error = \%args;
57 }# end if()
58
59 # Get the line of code that's breaking:
60 # And maybe a few before and after:
61 0 my $code;
62 0 ▶ if( -f $error->{file} )
Branch: 1 branch
BranchTrueFalseTotal
if (-f $error->{"file"}) 0 0 0
63 {
64 0 ▶ if( open my $ifh, '<', $error->{file} )
Branch: 1 branch
BranchTrueFalseTotal
if (open my $ifh, "<", $error->{"file"}) 0 0 0
65 {
66 0 my $max = 0;
67 0 ▶ $max++ while <$ifh>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $ifh) and do { ++$max } 0 0 0
68 0 close($ifh);
69 0 open $ifh, '<', $error->{file};
70
71 0 my $padding = 10;
72 0 my ($low, $high) = $class->_number_range($error->{line}, $max, $padding);
73  
74 0 my $line_number = 0;
75 0 my @lines = ( );
76 0 ▶ while( my $line = <$ifh> )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $ifh)) 0 0 0
77 {
78 0 $line_number++;
79 0 ▶ next unless $line_number >= $low;
Branch: 1 branch
BranchTrueFalseTotal
unless $line_number >= $low 0 0 0
80 0 ▶ last if $line_number > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $line_number > $high 0 0 0
81 0 push @lines, "line $line_number: $line";
82 }# end while()
83 0 close($ifh);
84 0 $code = join "", @lines;
85 }# end if()
86 }# end if()
87
88 my %info = (
89 # Defaults:
90 0 ▶ domain => eval { $Config->errors->domain } || $ENV{HTTP_HOST},
Condition0%0/4
&encode_json($Form) || '{}'
l!l
00
eval { do { &encode_json(\%session_data) } } || '{}'
l!l
00
MC/DC0%0/1
&encode_json($Form) || '{}'
&encode_json($Form)
MC/DC0%0/1
eval { do { &encode_json(\%session_data) } } || '{}'
eval { do { &encode_json(\%session_data) } }
Truth table0%0/2
A
A= &encode_json($Form)
Aresult
11
00
Truth table0%0/2
A
A= eval { do { &encode_json(\%session_data) } }
Aresult
11
00
91 request_uri => $ENV{REQUEST_URI},
92 file => $error->{file},
93 line => $error->{line},
94 message => $error->{message},
95 stacktrace => $error->{stacktrace},
96 code => $code,
97 form_data => encode_json($Form) || "{}",
98 0 ▶ session_data => eval { encode_json(\%session_data) } || "{}",
Condition0%0/2
$ENV{'REMOTE_ADDR'} || '127.0.0.1'
l!l
00
MC/DC0%0/1
$ENV{'REMOTE_ADDR'} || '127.0.0.1'
$ENV{'REMOTE_ADDR'}
Truth table0%0/2
A
A= $ENV{'REMOTE_ADDR'}
Aresult
11
00
99 http_referer => $ENV{HTTP_REFERER},
100 user_agent => $ENV{HTTP_USER_AGENT},
101 http_code => ($Response->Status =~ m{^(\d+)})[0],
102 0 ▶ remote_addr => $ENV{REMOTE_ADDR} || '127.0.0.1',
Condition0%0/3
eval { do { $Config->errors->domain } } || $ENV{'HTTP_HOST'}
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $Config->errors->domain } } || $ENV{'HTTP_HOST'}
eval { do { $Config->errors->domain } } $ENV{'HTTP_HOST'}
Truth table0%0/3
A || B
A= eval { do { $Config->errors->domain } }B= $ENV{'HTTP_HOST'}
ABresult
1X1
011
000
103 # Allow overrides:
104 %args
105 );
106
107 0 return bless \%info, $class;
108 }# end new()
109  
110  
111 0 ▶sub domain { $_[0]->{domain} }
Subroutine domain: not called CC=1 SCAR=6.9
Pod documented
112 0 ▶sub request_uri { $_[0]->{request_uri} }
Subroutine request_uri: not called CC=1 SCAR=6.9
Pod documented
113 0 ▶sub file { $_[0]->{file} }
Subroutine file: not called CC=1 SCAR=6.9
Pod documented
114 0 ▶sub line { $_[0]->{line} }
Subroutine line: not called CC=1 SCAR=6.9
Pod documented
115 0 ▶sub message { $_[0]->{message} }
Subroutine message: not called CC=1 SCAR=6.9
Pod documented
116 0 ▶sub stacktrace { $_[0]->{stacktrace} }
Subroutine stacktrace: not called CC=1 SCAR=6.9
Pod documented
117 0 ▶sub code { $_[0]->{code} }
Subroutine code: not called CC=1 SCAR=6.9
Pod documented
118 0 ▶sub form_data { $_[0]->{form_data} }
Subroutine form_data: not called CC=1 SCAR=6.9
Pod documented
119 0 ▶sub session_data { $_[0]->{session_data} }
Subroutine session_data: not called CC=1 SCAR=6.9
Pod documented
120 0 ▶sub http_referer { $_[0]->{http_referer} }
Subroutine http_referer: not called CC=1 SCAR=6.9
Pod documented
121 0 ▶sub user_agent { $_[0]->{user_agent} }
Subroutine user_agent: not called CC=1 SCAR=6.9
Pod documented
122 0 ▶sub http_code { $_[0]->{http_code} }
Subroutine http_code: not called CC=1 SCAR=6.9
Pod documented
123 0 ▶sub remote_addr { $_[0]->{remote_addr} }
Subroutine remote_addr: not called CC=1 SCAR=6.9
Pod documented
124  
125  
126 # Find the numbers within a given range, but not less than 1 and not greater than max.
127 sub _number_range
128 0 ▶{
Subroutine _number_range: not called CC=3 SCAR=24.8
129 0 my ($s, $number, $max, $padding) = @_;
130
131 0 ▶ my $low = $number - $padding > 0 ? $number - $padding : 1;
Branch: 1 branch
BranchTrueFalseTotal
$number - $padding > 0 ? : 0 0 0
132 0 ▶ my $high = $number + $padding <= $max ? $number + $padding : $max;
Branch: 1 branch
BranchTrueFalseTotal
$number + $padding <= $max ? : 0 0 0
133 0 return ($low, $high);
134 }# end _number_range()
135  
136 15 1;# return true:
137  
138 =pod
139  
140 =head1 NAME
141  
142 ASP4::Error - Representation of a server-side error
143  
144 =head1 SYNOPSIS
145  
146 use ASP4::Error;
147
148 # Pass in the $@ value after something dies or confesses:
149 eval { die "Foo" };
150 if( $@ ) {
151 my $error = ASP4::Error->new( $@ )
152 }
153
154 # Pass in your own info:
155 unless( $something ) {
156 my $error = ASP4::Error->new(
157 message => "If can, can. If no can, no can!"
158 );
159 }
160
161 =head1 DESCRIPTION
162  
163 ASP4 provides a simple means of dealing with errors. It emails them, by default,
164 to an email address you specify.
165  
166 Sometimes that is not convenient. Maybe you want to do something special with
167 the error - like log it someplace special.
168  
169 ASP4::Error is a simple representation of a server-side error.
170  
171 =head1 PUBLIC READ-ONLY PROPERTIES
172  
173 =head2 domain
174  
175 C<<$Config->errors->domain>> or C<$ENV{HTTP_HOST}>
176  
177 =head2 request_uri
178  
179 C<$ENV{REQUEST_URI}>
180  
181 =head2 file
182  
183 The name of the file in which the error occurred. This is gleaned from C<$@> unless C<file> is
184 passed to the constructor.
185  
186 =head2 line
187  
188 The line number within the file that the error occurred. This is gleaned from C<$@> unless
189 C<line> is passed to the constructor.
190  
191 =head2 code
192  
193 A string. Includes the 5 lines of code before and after the line of code where the error occurred.
194  
195 =head2 message
196  
197 Defaults to the first part of C<$@> unless otherwise specified.
198  
199 =head2 stacktrace
200  
201 A string - defaults to the value of C<$@>.
202  
203 =head2 form_data
204  
205 JSON-encoded C<$Form> object.
206  
207 =head2 session_data
208  
209 JSON-encoded C<$Session> object.
210  
211 =head2 http_referer
212  
213 Default value is C<$ENV{HTTP_REFERER}>
214  
215 =head2 user_agent
216  
217 Default value is C<$ENV{HTTP_USER_AGENT}>
218  
219 =head2 http_code
220  
221 The current value of C<<$Response->Status>>
222  
223 =head2 remote_addr
224  
225 Default value is C<$ENV{REMOTE_ADDR}> -- the IP address of the remote client.
226  
227 =head1 BUGS
228  
229 It's possible that some bugs have found their way into this release.
230  
231 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
232  
233 =head1 HOMEPAGE
234  
235 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
236 of ASP4 in action.
237  
238 =cut
239  
« blib/lib/ASP4/ModPerl.pm ↑ Summary blib/lib/ASP4/ConfigFinder.pm »