lib/WebDyne/Err.pm

filter: Statementstmt 30.7% 40 / 130 Branchbran 6.2% 2 / 32 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 66.6% 10 / 15 Podpod 0.0% 0 / 2 totaltotal 22.5% 52 / 231 CC 28 SCAR 61.9
CC
28
Cov
22%
err_html
756.0
err_eval
6.0
__ANON__
2.0
CRAP 486.6
« lib/WebDyne.pm ↑ Summary lib/WebDyne/Compile.pm »
1 #
2 # This file is part of WebDyne.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13 package WebDyne::Err;
14  
15  
16 # Compiler Pragma
17 #
18 3 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # Webmod Modules.
25 #
26 3 ▶use WebDyne::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use WebDyne::Err::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use WebDyne::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30  
31 # External modules
32 #
33 3 ▶use HTTP::Status qw(:constants is_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 $Data::Dumper::Indent=1;
37  
38  
39 # Version information
40 #
41 3 $VERSION='3.029';
42  
43  
44 # Debug
45 #
46 3 debug("%s loaded, version $VERSION", __PACKAGE__);
47  
48  
49 # Package wide vars
50 #
51 3 my %Package;
52  
53  
54 # Fix issues if mod_perl loads legacy Carp with modern Carp::Heavy
55 #
56 0 ▶{ my $cr=sub {return \@_};
Subroutine __ANON__: not called CC=1 SCAR=0.4
57 3 foreach my $method (qw(shortmess_real longmess_real shortmess_heavy longmess_heavy)) {
58 0 ▶ *{"Carp::${method}"}=sub {return @_}
Subroutine __ANON__: not called CC=1 SCAR=1.2
59 12 ▶ unless Carp->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless "Carp"->can($method) 6 6 12
60 }
61 }
62  
63  
64 # And done
65 #
66 3 1;
67  
68  
69 #------------------------------------------------------------------------------
70  
71  
72 0 ▶sub err_html {
Subroutine err_html: not called CC=27 SCAR=66.3
Pod undocumented
73  
74  
75 # Output errors to browser.
76 #
77 0 my ($self, $errstr)=@_;
78 0 $errstr=sprintf($errstr, @_[2..$#_]);
79  
80  
81 # Debug
82 #
83 0 debug("in error routine self: $self, errstr: $errstr, caller: %s", Dumper( [ (caller(0))[0..3] ]));
84  
85  
86 # Get errstr from stack if not supplied, or add if it
87 # has been
88 #
89 0 ▶ if ($errstr) {err($errstr)}
Branch: 1 branch
BranchTrueFalseTotal
if ($errstr) { } 0 0 0
90 else {
91 0 ▶ $errstr=errstr() || do {err($_='undefined error from handler'); $_}
Condition0%0/3
errstr() || do { err($_ = 'undefined error from handler'); $_ }
l!l&&r!l&&!r
000
MC/DC0%0/2
errstr() || do { err($_ = 'undefined error from handler'); $_ }
errstr() do { err($_ = 'undefined error from handler'); $_ }
Truth table0%0/3
A || B
A= errstr()B= do { err($_ = 'undefined error from handler'); $_ }
ABresult
1X1
011
000
92 }
93  
94 #$errstr ? err($errstr) : ($errstr=errstr() || do {err($_='undefined error from handler'); $_});
95 0 debug("final errstr: $errstr");
96  
97  
98 # Try to get request handler;
99 #
100 0 my $r;
101 0 ▶ if ($r=eval {$self->{'_r'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r = eval { do { $self->{"_r"} } }) 0 0 0
102  
103 # Get main request handler in case we are in subrequest
104 #
105 0 ▶ $r=$r->main() || $r;
Condition0%0/3
$r->main || $r
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->main || $r
$r->main $r
Truth table0%0/3
A || B
A= $r->mainB= $r
ABresult
1X1
011
000
106  
107 }
108 0 debug("r $r");
109  
110  
111 # Print errstr and exit immediately if no request object yet, or in error loop - something
112 # is seriously wrong;
113 #
114 0 ▶ if (!$r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 0 0
115 0 print(errdump());
116 0 CORE::exit 0;
117 }
118  
119  
120 # Try to get CGI object from class, or create if not present - may
121 # not have been initialised before error occured);
122 #
123 0 ▶ my $cgi_or=$self->{'_CGI'} || CGI::Simple->new($r);
Condition0%0/3
$self->{'_CGI'} || 'CGI::Simple'->new($r)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_CGI'} || 'CGI::Simple'->new($r)
$self->{'_CGI'} 'CGI::Simple'->new($r)
Truth table0%0/3
A || B
A= $self->{'_CGI'}B= 'CGI::Simple'->new($r)
ABresult
1X1
011
000
124 0 debug("cgi_or $cgi_or");
125  
126  
127 # Log the error
128 #
129 0 $r->log_error($errstr);
130  
131  
132 # Status must be internal error if not set to something else already
133 #
134 0 debug('existing status: %s', $r->status());
135 0 ▶ unless ($r->status() && (is_error($r->status()))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->status and &is_error(scalar $r->status)) 0 0 0
Condition0%0/3
$r->status and &is_error(scalar $r->status)
!ll&&!rl&&r
000
MC/DC0%0/2
$r->status and &is_error(scalar $r->status)
$r->status &is_error(scalar $r->status)
Truth table0%0/3
A and B
A= $r->statusB= &is_error(scalar $r->status)
ABresult
0X0
100
111
136 0 debug('setting status to: %s', HTTP_INTERNAL_SERVER_ERROR);
137 0 $r->status(HTTP_INTERNAL_SERVER_ERROR);
138 }
139  
140  
141 # Do not run any more handlers
142 #
143 0 $r->set_handlers(PerlHandler => undef);
144  
145  
146 # Optionally kill this Apache process afterwards to make sure it does
147 # not behave badly after this error, if that is what the user has
148 # configured
149 #
150 0 ▶ if ($WEBDYNE_ERROR_EXIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebDyne::Err::WEBDYNE_ERROR_EXIT) 0 0 0
151 0 ▶ my $cr=sub {CORE::exit()};
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 ▶ $MP2 ? $r->pool->cleanup_register($cr) : $r->register_cleanup($cr);
Branch: 1 branch
BranchTrueFalseTotal
$WebDyne::Err::MP2 ? : 0 0 0
153 }
154  
155  
156 # Error can be text or HTML, must be text if in Safe eval mode
157 #
158 0 debug("WEBDYNE_ERROR_TEXT: $WEBDYNE_ERROR_TEXT");
159 #if ($WEBDYNE_ERROR_TEXT || $WEBDYNE_EVAL_SAFE || $self->{'_error_handler_run'}++ || !$cgi_or || UNIVERSAL::can($self, 'debug')) {
160 0 ▶ if ($WEBDYNE_ERROR_TEXT || $WEBDYNE_EVAL_SAFE || $self->{'_error_handler_run'}++ || !$cgi_or) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebDyne::Err::WEBDYNE_ERROR_TEXT or $WebDyne::Err::WEBDYNE_EVAL_SAFE or $self->{'_error_handler_run'}++ or not $cgi_or) { } 0 0 0
Condition0%0/9
$WebDyne::Err::WEBDYNE_ERROR_TEXT or $WebDyne::Err::WEBDYNE_EVAL_SAFE or $self->{"_error_handler_run"}++ or not $cgi_or
l!l&&r!l&&!r
000
$WebDyne::Err::WEBDYNE_ERROR_TEXT or $WebDyne::Err::WEBDYNE_EVAL_SAFE or $self->{"_error_handler_run"}++
l!l&&r!l&&!r
000
$WebDyne::Err::WEBDYNE_ERROR_TEXT or $WebDyne::Err::WEBDYNE_EVAL_SAFE
l!l&&r!l&&!r
000
MC/DC0%0/4
$WebDyne::Err::WEBDYNE_ERROR_TEXT or $WebDyne::Err::WEBDYNE_EVAL_SAFE or $self->{"_error_handler_run"}++ or not $cgi_or
$WebDyne::Err::WEBDYNE_ERROR_TEXT $WebDyne::Err::WEBDYNE_EVAL_SAFE $self->{"_error_handler_run"}++ not $cgi_or
Truth table0%0/5
A or B or C or not(D)
A= $WebDyne::Err::WEBDYNE_ERROR_TEXTB= $WebDyne::Err::WEBDYNE_EVAL_SAFEC= $self->{"_error_handler_run"}++D= not $cgi_or
ABCDresult
1XXX1
01XX1
001X1
00011
00000
161  
162  
163 # Text error, set content type
164 #
165 debug(
166 "using text error; WEBDYNE_ERROR_TEXT: %s, WEBDYNE_EVAL_SAFE: %s, _error_handler_run: %s, cgi_or: %s, updating $r content_type",
167 0 $WEBDYNE_ERROR_TEXT, $WEBDYNE_EVAL_SAFE, $self->{'_error_handler_run'}, $cgi_or
168 );
169 #$r->content_type('text/plain');
170 0 debug('existing content type: %s', $r->content_type);
171 0 $r->content_type($WEBDYNE_CONTENT_TYPE_TEXT);
172 0 debug('updated content type: %s', $r->content_type);
173
174  
175 # Push error
176 #
177 0 my $err_text=errdump(
178 {
179  
180 'URI' => $r->uri(),
181 #'Line' => scalar $self->data_ar_html_line_no(pop @{$self->{'_data_ar_err'}}),
182 'Line' => scalar $self->data_ar_html_line_no(),
183  
184 });
185  
186  
187 # Clear error stack and $@.
188 #
189 0 ▶ errclr(); eval {undef} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
190  
191  
192 # Print error and return
193 #
194 0 ▶ $r->send_http_header() if !$MP2;
Branch: 1 branch
BranchTrueFalseTotal
unless $WebDyne::Err::MP2 0 0 0
195 0 if (1) {
196 0 my $status=$r->status();
197 0 ▶ unless($status && is_error($status)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status and &is_error($status)) 0 0 0
Condition0%0/3
$status and &is_error($status)
!ll&&!rl&&r
000
MC/DC0%0/2
$status and &is_error($status)
$status &is_error($status)
Truth table0%0/3
A and B
A= $statusB= &is_error($status)
ABresult
0X0
100
111
198 0 $r->status($status=HTTP_INTERNAL_SERVER_ERROR);
199 };
200 0 $r->custom_response($status, $err_text);
201 0 $r->print($err_text);
202 0 return $status;
203 }
204 else {
205 $r->custom_response(HTTP_INTERNAL_SERVER_ERROR, $err_text);
206 $r->status(HTTP_INTERNAL_SERVER_ERROR);
207 $r->print($err_text);
208 return HTTP_INTERNAL_SERVER_ERROR;
209 }
210  
211 }
212 else {
213  
214  
215 # Get error parameters, must make copy of stack, data block - they will be erased.
216 #
217 0 debug('using html error');
218 0 my @errstack=@{&errstack()};
219 my %param=(
220  
221 errstr => $errstr,
222 errstack_ar => \@errstack,
223 err_eval_perl_sr => $self->{'_err_eval_perl_sr'},
224 err_eval_line => $self->{'_err_eval_line'},
225 0 data_ar_err => $self->{'_data_ar_err'},
226  
227 );
228  
229  
230 # Clear error stack and $@ so this render works without errors
231 #
232 0 ▶ errclr(); eval {undef} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
233  
234  
235 # Wrap everything in eval block in case this error was thrown interally by
236 # WebDyne not being able to load/start etc, in which case trying to run it
237 # again won't be helpful
238 #
239 0 my $status;
240 0 eval {
241  
242  
243 # Only compile container once if we can help it
244 #
245 0 local $SIG{__DIE__};
246 0 require WebDyne::Compile;
247 0 ▶ my $container_ar=(
Condition0%0/2
$self->WebDyne::Compile::compile({'srce', $WebDyne::Err::WEBDYNE_ERR_TEMPLATE, 'no_filter', 1}) || (return $self->err_html('fatal problem in error handler during compile !'))
l!l
00
MC/DC0%0/1
$self->WebDyne::Compile::compile({'srce', $WebDyne::Err::WEBDYNE_ERR_TEMPLATE, 'no_filter', 1}) || (return $self->err_html('fatal problem in error handler during compile !'))
$self->WebDyne::Compile::compile({'srce', $WebDyne::Err::WEBDYNE_ERR_TEMPLATE, 'no_filter', 1})
Truth table0%0/2
A
A= $self->WebDyne::Compile::compile({'srce', $WebDyne::Err::WEBDYNE_ERR_TEMPLATE, 'no_filter', 1})
Aresult
11
00
248  
249 # Don't cache it - only minor penalty to recompile and WEBDYNE_RELOAD=1 breaks error handler
250 # if multiple errors.
251 #$Package{'container_ar'} ||= &WebDyne::Compile::compile(
252 $self->WebDyne::Compile::compile({
253  
254 srce => $WEBDYNE_ERR_TEMPLATE,
255 no_filter => 1
256  
257 })) || return $self->err_html('fatal problem in error handler during compile !');
258  
259  
260 # Get the data portion of the container (meta info not needed) and render. Bit of cheating
261 # to use internal
262 #
263 0 my $data_ar=$container_ar->[$WEBDYNE_CONTAINER_DATA_IX];
264 0 debug("err_html data_ar: %s", Dumper($param{'data_ar'}));
265  
266  
267 # Reset render state and render error page
268 #
269 0 $self->render_reset($data_ar);
270 #my $html_sr=$self->render({
271 ##
272 # data => $data_ar,
273 # param => \%param
274 #
275 # }) || return $self->err_html('fatal problem in error handler during render: %s !', errstr() || 'undefined error');
276 0 ▶ my $html_sr=$self->render_data_ar(
Condition0%0/4
$self->render_data_ar('data', $data_ar, 'param', \%param) || (return $self->err_html('fatal problem in error handler during render: %s !', errstr() || 'undefined error'))
l!l
00
errstr() || 'undefined error'
l!l
00
MC/DC0%0/1
$self->render_data_ar('data', $data_ar, 'param', \%param) || (return $self->err_html('fatal problem in error handler during render: %s !', errstr() || 'undefined error'))
$self->render_data_ar('data', $data_ar, 'param', \%param)
MC/DC0%0/1
errstr() || 'undefined error'
errstr()
Truth table0%0/2
A
A= $self->render_data_ar('data', $data_ar, 'param', \%param)
Aresult
11
00
Truth table0%0/2
A
A= errstr()
Aresult
11
00
277  
278 data => $data_ar,
279 param => \%param
280  
281 ) || return $self->err_html('fatal problem in error handler during render: %s !', errstr() || 'undefined error');
282  
283  
284 # Set custom handler
285 #
286 0 $status=$r->status();
287 0 debug("send custom response for status $status on r $r");
288 0 $r->custom_response($status, ${$html_sr});
289  
290  
291 # Clear error stack again, make sure all is clean before we return.
292 #
293 0 ▶ errclr(); eval {} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
294  
295 };
296  
297  
298 # Check if render went OK, if not revert to text - better than
299 # showing nothing ..
300 #
301 0 ▶ if ($@ || !$status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $status) 0 0 0
Condition0%0/3
$@ or not $status
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $status
$@ not $status
Truth table0%0/3
A or not(B)
A= $@B= not $status
ABresult
1X1
011
000
302 0 debug("unable to render HTML template, reverting to text");
303 0 ▶ err($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
304 0 err('previous error stack %s', Data::Dumper::Dumper(\@errstack));
305 0 my $webdyne_error_text_save=$WEBDYNE_ERROR_TEXT;
306 0 $WEBDYNE_ERROR_TEXT=1;
307 0 $status=$self->err_html($errstr);
308 0 $WEBDYNE_ERROR_TEXT=$webdyne_error_text_save;
309  
310 }
311  
312 # Return result
313 #
314 0 debug("return status: $status");
315 0 return $status
316  
317 }
318  
319 }
320  
321  
322 0 ▶sub err_eval {
Subroutine err_eval: not called CC=2 SCAR=17.9
Pod undocumented
323  
324 # Special handler for eval errors
325 #
326 0 my ($self, $message, $perl_sr, $inode)=@_;
327 0 debug("err_eval: %s, perl_sr: %s, caller %s", $message, Dumper($perl_sr), Dumper([caller()]));
328  
329  
330 # Try to scrape line from message
331 #
332 0 my ($err_eval_line)=($message=~/WebDyne::${inode}\s+line\s+(\d+)/);
333 0 ▶ unless ($err_eval_line) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err_eval_line) 0 0 0
334  
335 # Only if Devel::Confess installed will this parse work
336 #
337 # Illegal division by zero at (eval 211)[WebDyne::6d8da02b63e4707b80466fda560173a6:5] line 1.
338 #
339 0 ($err_eval_line)=($message=~/\[WebDyne::${inode}:(\d+)\]/);
340 }
341 0 debug("err_eval eval_line:$err_eval_line");
342  
343  
344 # Store away for future ref by error handler
345 #
346 0 $self->{'_err_eval_perl_sr'}=$perl_sr;
347 0 $self->{'_err_eval_line'}=$err_eval_line;
348  
349  
350 # Send message off to main error handler and return
351 #
352 #return &errsubst($message);
353 0 return err($message);
354  
355 }
356  
357 __END__
« lib/WebDyne.pm ↑ Summary lib/WebDyne/Compile.pm »