File Coverage

blib/lib/Exception/Handler.pm
Criterion Covered Total %
statement 9 50 18.0
branch 0 22 0.0
condition 0 2 0.0
subroutine 3 8 37.5
pod 0 4 0.0
total 12 86 13.9


line stmt bran cond sub pod time code
1             package Exception::Handler;
2 4     4   39583 use strict;
  4         11  
  4         242  
3 4     4   21 use vars qw( $VERSION );
  4         7  
  4         4045  
4             $VERSION = 1.00_4; # Thu Dec 21 18:04:23 CST 2006
5              
6             # --------------------------------------------------------
7             # Constructor
8             # --------------------------------------------------------
9             sub new {
10 3     3 0 37 my($this) = bless({ }, shift(@_));
11 3         21 $this->{'errors'} = [@_];
12 3         16 return $this
13             }
14              
15             # --------------------------------------------------------
16             # Exception::Handler::error()
17             # --------------------------------------------------------
18 0     0 0   sub error { @{ $_->{'errors'} } } # very bad; very easy
  0            
19              
20              
21             # --------------------------------------------------------
22             # Exception::Handler::fail()
23             # --------------------------------------------------------
24             sub fail {
25              
26 0     0 0   my($this) = shift(@_);
27 0   0       my($throw_count) = $this->{'tflag'} || 0;
28              
29             {
30             # I refuse to manually initialize a standard environment
31             # variable. This is an example where the warnings pragma
32             # is going too far. It's something we live with.
33 0           local($^W) = undef;
  0            
34              
35             # if we're running in a CGI gateway iface, we need
36             # to output the necessary HTTP headers
37 0 0         if ( $ENV{'REQUEST_METHOD'} ) {
38              
39 0 0         print(<<__crash__) and exit;
40             Content-Type: text/html; charset=ISO-8859-1
41              
42            
 
43             PROCESS TERMINATED DUE TO ERRORS
44 0           @{[ $this->trace(@_) ]}
45            
46             __crash__
47             }
48             else {
49              
50 0 0         print(<<__crash__) and exit;
51             PROCESS TERMINATED DUE TO ERRORS
52 0           @{[ $this->trace(@_) ]}
53             __crash__
54             }
55             }
56              
57             exit
58 0           }
59              
60              
61             # --------------------------------------------------------
62             # Exception::Handler::trace()
63             # --------------------------------------------------------
64             sub trace {
65              
66 0     0 0   my($this) = shift(@_);
67 0           my(@errors) = @_; $this->{'errors'} = [@errors];
  0            
68 0           my($errfile) = '';
69 0           my($caught) = '';
70             my(
71 0           $pak, $file, $line, $sub,
72             $hasargs, $wantarray, $evaltext, $req_OR_use,
73             @stack, $i, $ialias
74             );
75              
76 0           $ialias = 0;
77              
78 0           while (
79             (
80             $pak, $file, $line, $sub,
81             $hasargs, $wantarray, $evaltext, $req_OR_use
82             ) = caller( $i++ )
83             )
84             {
85 0 0         $ialias = $i - 2; next unless ($ialias > 0);
  0            
86              
87 0 0         if ( (split(/\:\:/, $sub))[0] ne __PACKAGE__ ) {
88              
89 0           push @stack, <<__ERR__
90 0 0         $ialias. $sub
91             -called at line ($line) of $file
92 0 0         @{[ ($hasargs)
93             ? '-was called with args'
94             : '-was called without args' ]}
95             @{[ ($evaltext)
96             ? '-was called to evalate text'
97             : '-was not called to evaluate anything' ]}
98             __ERR__
99             }
100             else {
101 0           $caught = qq[\012] . uc(qq[exception was raised at])
102             . qq[ line ($line) of $file];
103             }
104             }
105              
106 0           $i = 0;
107              
108 0 0         if ( scalar(@errors) == 0 ) {
109              
110 0           push ( @errors, qq[[Unspecified error. Frame no. $ialias...]] );
111             }
112              
113 0           foreach (@errors) {
114              
115 0 0         $_ = ( defined($_) ) ? $_ : '';
116              
117 0 0         if (!length($_)) { $_ = qq[Something is wrong. Frame no. $ialias...]; }
  0            
118             else {
119              
120 0           $_ =~ s/^(?:\r|\n)//o; $_ =~ s/(?:\r|\n)$//o;
  0            
121              
122 0           $_ = qq[\012$_\012];
123             }
124              
125 0           ++$i;
126             }
127              
128 0 0         join(qq[\012] x 2, @errors)
129             . ($caught ? $caught . qq[\012] : '')
130             . qq[\012] . join(qq[\012] x 2, @stack);
131             }
132              
133              
134             # --------------------------------------------------------
135             # Exception::Handler::DESTROY()
136             # --------------------------------------------------------
137 0     0     sub DESTROY { } sub AUTOLOAD { }
  0     0      
138             1;
139              
140             =pod
141              
142             =head1 NAME
143              
144             Exception::Handler - Report exceptions with formatted text call-stack
145              
146             =head1 VERSION
147              
148             1.00_2
149              
150             =head1 @EXPORT, @EXPORT_OK
151              
152             None.
153              
154             =head1 Methods
155              
156             new()
157             fail()
158             trace()
159             error()
160              
161             =head2 AUTOLOAD-ed methods
162              
163             None.
164              
165             =head1 PREREQUISITES
166              
167             None.
168              
169             =head1 AUTHOR
170              
171             Tommy Butler
172              
173             =head1 COPYRIGHT
174              
175             Copyright(c) 2001-2003, Tommy Butler. All rights reserved.
176              
177             =head1 LICENSE
178              
179             This library is free software, you may redistribute
180             and/or modify it under the same terms as Perl itself.
181              
182             =cut
183