File Coverage

blib/lib/XML/SAX/Exception.pm
Criterion Covered Total %
statement 28 42 66.6
branch 6 10 60.0
condition n/a
subroutine 7 9 77.7
pod 0 5 0.0
total 41 66 62.1


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