blib/lib/CGI/ClientError.pm

filter: Statementstmt 36.6% 11 / 30 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 20.0% 2 / 10 Podpod 0.0% 0 / 8 totaltotal 19.4% 13 / 67 CC 9 SCAR 29.6
CC
9
Cov
23%
error
42.0
printfile
20.0
setheaderfile
2.0
CRAP 19.2
↑ Summary
1 package CGI::ClientError;
2 1 ▶use vars qw($VERSION @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 @ISA=qw(Exporter);
5  
6 1 @EXPORT=qw(cgi_report_error);
7  
8 1 $VERSION=0.03;
9  
10 =head1 NAME
11  
12 CGI::ClientError - send minimalistic error messages to the browser
13  
14 =head1 SYNOPSIS
15  
16 use CGI::ClientError;
17 &CGI::ClientError::setheaderfile('/path/to/some/header');
18 &CGI::ClientError::setfooterfile('/path/to/some/footer');
19 &CGI::ClientError::setheader("Content-Type: text/plain\n\nYou've done something wrong: ");
20 &CGI::ClientError::setfooter("If this is unclear, go hang yourself.");
21  
22 &CGI::ClientError::sethandler(sub { die; });
23  
24 (...)
25  
26 if (clientisadork) { &CGI::ClientError::error("You are a dork!");
27 # or
28 if (clientisadork) { &cgi_report_error("You are a dork!");
29  
30 =head1 DESCRIPTION
31  
32 Errors might appear in a CGI. If the script knows what is wrong, it
33 should tell what is wrong. But I think it's important to separate
34 between when it should tell the client, and when it should tell the
35 webmaster. The user/client shouldn't be get error messages that are
36 irrelevant or meaningless or even possibly exploitable - as "out of
37 disk", "out of memory", "core dumped", etc. Instead, the script
38 should die, the error should be logged, and perhaps even sent by mail
39 to the webmaster - and perhaps even to his cellphone. The user should
40 get an 500 and a clear, friendly message that the problem is at the
41 server side and probably will be dealt with ("try again later, or mail
42 webmaster").
43  
44 Anyway, sometimes the client is to blame for the error. He has typed
45 in a text string in a number box, he claims beeing born in 2019-14-14,
46 he has been typing in a long URL with illegal parameters, etc. Then
47 the client should get an informative error message. That's what this
48 small module is for.
49  
50 Three variables might be set by the caller program, header, footer and
51 handler. The header and footer is what to output before and after the
52 error message. The default header is:
53 Content-Type: text/html
54 E<lt>H1E<gt>ErrorE<lt>/h1E<gt>
55 Here is an error message for you:E<lt>brE<gt>
56 E<lt>iE<gt>
57  
58 The default footer is:
59 E<lt>/iE<gt>E<lt>brE<gt>
60 If something is unclear, feel free to contact the
61 webmaster.
62  
63 The default handler is ... do nothing.
64  
65 Somebody has probably written scientific papers about how to be
66 respectfully and pedagogic when telling a user that he has done an
67 error. I think it is wise to be humble, don't expect too much -
68 remember, the average web user of today is not a typical unix user. I
69 don't know. I don't care.
70  
71 This module probably stinks - but the idea itself doesn't; I think
72 it's proper to use "die" if it's a real server error, and some other
73 sub / method if it's actually a client error.
74  
75 =head1 AUTHOR
76  
77 Tobias Brox <tobix@irctos.org>
78  
79 =cut
80  
81 0 ▶sub setheaderfile {
Subroutine setheaderfile: not called CC=1 SCAR=6.9
Pod undocumented
82 0 $CGI::ClientError::headerfile=shift;
83 }
84  
85 0 ▶sub setfooterfile {
Subroutine setfooterfile: not called CC=1 SCAR=6.9
Pod undocumented
86 0 $CGI::ClientError::footerfile=shift;
87 }
88  
89 0 ▶sub setheader {
Subroutine setheader: not called CC=1 SCAR=6.9
Pod undocumented
90 0 $CGI::ClientError::header=shift;
91 }
92  
93 0 ▶sub setfooter {
Subroutine setfooter: not called CC=1 SCAR=6.9
Pod undocumented
94 0 $CGI::ClientError::footer=shift;
95 }
96  
97 0 ▶sub sethandler {
Subroutine sethandler: not called CC=1 SCAR=6.9
Pod undocumented
98 0 $CGI::ClientError::handler=shift;
99 }
100  
101 0 ▶sub printfile {
Subroutine printfile: not called CC=4 SCAR=30.0
Pod undocumented
102 0 my $filename=shift;
103 0 ▶ $filename || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$filename or return 0 0 0 0
104 0 ▶ open(FILE, "<$filename") || return 0;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$filename" or return 0 0 0 0
105 0 ▶ while (<FILE>) {print;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
106 0 close(FILE);
107 0 return 1;
108 }
109  
110 0 ▶sub cgi_report_error {
Subroutine cgi_report_error: not called CC=1 SCAR=6.9
Pod undocumented
111 0 error(@_);
112 }
113  
114 0 ▶sub error {
Subroutine error: not called CC=6 SCAR=37.4
Pod undocumented
115 0 ▶ &printfile($CGI::ClientError::headerfile) or
Branch: 1 branch
BranchTrueFalseTotal
&printfile($headerfile) or print $header 0 0 0
116 print $CGI::ClientError::header;
117  
118 # In case we're called as a class method:
119 0 ▶ if (ref $_[0] || $_[0] =~ /^((\w+)\:\:(\w+))$/) { shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] or $_[0] =~ /^((\w+)\:\:(\w+))$/) 0 0 0
Condition0%0/3
ref $_[0] or $_[0] =~ /^((\w+)\:\:(\w+))$/
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] or $_[0] =~ /^((\w+)\:\:(\w+))$/
ref $_[0] $_[0] =~ /^((\w+)\:\:(\w+))$/
Truth table0%0/3
A or B
A= ref $_[0]B= $_[0] =~ /^((\w+)\:\:(\w+))$/
ABresult
1X1
011
000
120  
121 0 print shift;
122  
123 0 ▶ &printfile($CGI::ClientError::footerfile) or
Branch: 1 branch
BranchTrueFalseTotal
&printfile($footerfile) or print $footer 0 0 0
124 print $CGI::ClientError::footer;
125  
126 0 ▶ defined $CGI::handler && &$CGI::Handler;
Branch: 1 branch
BranchTrueFalseTotal
defined $CGI::handler and &$CGI::Handler 0 0 0
127 }
128  
129 1 $CGI::ClientError::header="Content-Type: text/html
130  
131 <H1>Error</h1>
132 Here is an error message for you:<br>
133 <i>";
134  
135 1 $CGI::ClientError::footer="</i><br>
136 If something is unclear, feel free to contact the
137 webmaster.\n";
138  
139  
140  
141  
142  
143  
144  
↑ Summary