File Coverage

lib/Egg/Exception.pm
Criterion Covered Total %
statement 38 38 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod n/a
total 49 50 98.0


line stmt bran cond sub pod time code
1             package Egg::Exception;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Exception.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 37     37   213 use strict;
  37         84  
  37         1456  
8 37     37   348 use warnings;
  37         73  
  37         1877  
9              
10             our $VERSION= '3.00';
11              
12             package Egg::Error;
13 37     37   235 use strict;
  37         75  
  37         1363  
14 37     37   203 use warnings;
  37         68  
  37         1391  
15 37     37   43799 use Devel::StackTrace;
  37         166731  
  37         401  
16 37     37   1338 use overload '""' => 'stacktrace';
  37         94  
  37         196  
17 37     37   2637 use base qw/ Class::Accessor::Fast /;
  37         89  
  37         54620  
18              
19             our $IGNORE_PACKAGE= [qw/ main Carp /];
20             our $IGNORE_CLASS = [qw/ Egg::Error /];
21              
22             __PACKAGE__->mk_accessors(qw/ errstr frames as_string /);
23              
24             sub new {
25 186     186   378 my $class = shift;
26 186         662 my $errstr= join '', @_;
27 186         255 my $stacktrace;
28             {
29 186         289 local $@;
  186         265  
30 186         382 eval{
31 186         1063 $stacktrace= Devel::StackTrace->new(
32             ignore_package => $IGNORE_PACKAGE,
33             ignore_class => $IGNORE_CLASS,
34             no_refs => 1,
35             respect_overload => 1,
36             );
37             };
38             };
39 186 50       79599 die $errstr unless $stacktrace;
40 186         154605 bless {
41             errstr => $errstr,
42             as_string=> $stacktrace->as_string,
43             frames => [$stacktrace->frames],
44             }, $class;
45             }
46             sub throw {
47 186     186   621 my $error= shift->new(@_);
48 186         39138 die $error;
49             }
50             sub stacktrace {
51 229     229   464 my($self)= @_;
52 229         309 my @trace;
53 229         317 foreach my $f (@{$self->frames}) {
  229         1011  
54 643         5599 push @trace, $f->filename. ': '. $f->line;
55             }
56 229         88513 "$self->{errstr} \n\n stacktrace: \n [". join("] \n [", @trace). "] \n";
57             }
58              
59             1;
60              
61             __END__
62              
63             =head1 NAME
64              
65             Egg::Exception - The exception with stack trace is generated.
66              
67             =head1 SYNOPSIS
68              
69             use Egg::Exception;
70            
71             Egg::Error->throw('The error occurs.');
72            
73             or
74            
75             local $SIG{__DIE__}= sub { Egg::Error->throw(@_) };
76             die 'The error occurs.';
77              
78             =head1 DESCRIPTION
79              
80             It is a module to vomit the message with stack trace when the exception is generated.
81              
82             =head1 METHODS
83              
84             =head2 new
85              
86             Constructor. This is internally called.
87              
88             =head2 throw ([MESSAGE_STRING])
89              
90             After the constructor is let pass, the exception is generated.
91              
92             Egg::Error->throw( 'internal error.' );
93              
94             =head2 stacktrace
95              
96             Only trace information on the object is returned.
97              
98             local $SIG{__DIE__}= sub { Egg::Error->throw(@_) };
99             eval{ ... code. };
100             if ($@) { die $@->stacktrace }
101              
102             =head2 frames
103              
104             Trace information on the object is returned by the ARRAY reference.
105              
106             local $SIG{__DIE__}= sub { Egg::Error->throw(@_) };
107             eval{ ... code. };
108             if ($@) { die join "\n", @{$@->frames} }
109              
110             =head2 as_string
111              
112             as_string of L<Devel::StackTrace > is returned.
113              
114             local $SIG{__DIE__}= sub { Egg::Error->throw(@_) };
115             eval{ ... code. };
116             if ($@) { die $@->as_string }
117              
118             =head2 errstr
119              
120             Only the exception message of the object is returned.
121              
122             local $SIG{__DIE__}= sub { Egg::Error->throw(@_) };
123             eval{ ... code. };
124             if ($@) { die $@->errstr }
125              
126             =head1 SEE ALSO
127              
128             L<Egg::Release>,
129             L<Devel::StackTrace>,
130              
131             =head1 AUTHOR
132              
133             Masatoshi Mizuno, E<lt>lusheE<64>cpan.orgE<gt>
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
138              
139             This library is free software; you can redistribute it and/or modify
140             it under the same terms as Perl itself, either Perl version 5.8.6 or,
141             at your option, any later version of Perl 5 you may have available.
142              
143             =cut
144