File Coverage

blib/lib/XML/SAX/Exception.pm
Criterion Covered Total %
statement 29 43 67.4
branch 6 10 60.0
condition n/a
subroutine 8 10 80.0
pod 0 5 0.0
total 43 68 63.2


line stmt bran cond sub pod time code
1             package XML::SAX::Exception;
2             BEGIN {
3 18     18   20366 $XML::SAX::Exception::VERSION = '1.08';
4             }
5              
6 18     18   99 use strict;
  18         41  
  18         666  
7              
8 18         110 use overload '""' => "stringify",
9 18     18   45580 'fallback' => 1;
  18         23152  
10              
11 18     18   1656 use vars qw($StackTrace);
  18         35  
  18         865  
12              
13 18     18   95 use Carp;
  18         30  
  18         13455  
14              
15             $StackTrace = $ENV{XML_DEBUG} || 0;
16              
17             # Other exception classes:
18              
19             @XML::SAX::Exception::NotRecognized::ISA = ('XML::SAX::Exception');
20             @XML::SAX::Exception::NotSupported::ISA = ('XML::SAX::Exception');
21             @XML::SAX::Exception::Parse::ISA = ('XML::SAX::Exception');
22              
23              
24             sub throw {
25 4     4 0 1662 my $class = shift;
26 4 50       12 if (ref($class)) {
27 0         0 die $class;
28             }
29 4         25 die $class->new(@_);
30             }
31              
32             sub new {
33 4     4 0 7 my $class = shift;
34 4         16 my %opts = @_;
35 4 50       14 confess "Invalid options: " . join(', ', keys %opts) unless exists $opts{Message};
36            
37 4 50       41 bless { ($StackTrace ? (StackTrace => stacktrace()) : ()), %opts },
38             $class;
39             }
40              
41             sub stringify {
42 10     10 0 1552 my $self = shift;
43 10         28 local $^W;
44 10         12 my $error;
45 10 100       25 if (exists $self->{LineNumber}) {
46 3         12 $error = $self->{Message} . " [Ln: " . $self->{LineNumber} .
47             ", Col: " . $self->{ColumnNumber} . "]";
48             }
49             else {
50 7         15 $error = $self->{Message};
51             }
52 10 50       25 if ($StackTrace) {
53 0         0 $error .= stackstring($self->{StackTrace});
54             }
55 10         14 $error .= "\n";
56 10         40 return $error;
57             }
58              
59             sub stacktrace {
60 0     0 0   my $i = 2;
61 0           my @fulltrace;
62 0           while (my @trace = caller($i++)) {
63 0           my %hash;
64 0           @hash{qw(Package Filename Line)} = @trace[0..2];
65 0           push @fulltrace, \%hash;
66             }
67 0           return \@fulltrace;
68             }
69              
70             sub stackstring {
71 0     0 0   my $stacktrace = shift;
72 0           my $string = "\nFrom:\n";
73 0           foreach my $current (@$stacktrace) {
74 0           $string .= $current->{Filename} . " Line: " . $current->{Line} . "\n";
75             }
76 0           return $string;
77             }
78              
79             1;
80              
81             __END__