File Coverage

blib/lib/CGI/ClientError.pm
Criterion Covered Total %
statement 6 25 24.0
branch 0 12 0.0
condition 0 3 0.0
subroutine 2 10 20.0
pod 0 8 0.0
total 8 58 13.7


line stmt bran cond sub pod time code
1             package CGI::ClientError;
2 1     1   580 use vars qw($VERSION @EXPORT);
  1         2  
  1         57  
3 1     1   5 use Exporter;
  1         1  
  1         508  
4             @ISA=qw(Exporter);
5              
6             @EXPORT=qw(cgi_report_error);
7              
8             $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             EH1EErrorE/h1E
55             Here is an error message for you:EbrE
56             EiE
57              
58             The default footer is:
59             E/iEEbrE
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
78              
79             =cut
80              
81             sub setheaderfile {
82 0     0 0   $CGI::ClientError::headerfile=shift;
83             }
84              
85             sub setfooterfile {
86 0     0 0   $CGI::ClientError::footerfile=shift;
87             }
88              
89             sub setheader {
90 0     0 0   $CGI::ClientError::header=shift;
91             }
92              
93             sub setfooter {
94 0     0 0   $CGI::ClientError::footer=shift;
95             }
96              
97             sub sethandler {
98 0     0 0   $CGI::ClientError::handler=shift;
99             }
100              
101             sub printfile {
102 0     0 0   my $filename=shift;
103 0 0         $filename || return 0;
104 0 0         open(FILE, "<$filename") || return 0;
105 0           while () {print;}
  0            
106 0           close(FILE);
107 0           return 1;
108             }
109              
110             sub cgi_report_error {
111 0     0 0   error(@_);
112             }
113              
114             sub error {
115 0 0   0 0   &printfile($CGI::ClientError::headerfile) or
116             print $CGI::ClientError::header;
117              
118             # In case we're called as a class method:
119 0 0 0       if (ref $_[0] || $_[0] =~ /^((\w+)\:\:(\w+))$/) { shift; }
  0            
120              
121 0           print shift;
122              
123 0 0         &printfile($CGI::ClientError::footerfile) or
124             print $CGI::ClientError::footer;
125              
126 0 0         defined $CGI::handler && &$CGI::Handler;
127             }
128              
129             $CGI::ClientError::header="Content-Type: text/html
130              
131            

Error

132             Here is an error message for you:
133             ";
134              
135             $CGI::ClientError::footer="
136             If something is unclear, feel free to contact the
137             webmaster.\n";
138              
139              
140              
141              
142              
143              
144