File Coverage

blib/lib/Eve/Exception.pm
Criterion Covered Total %
statement 17 17 100.0
branch 3 4 75.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 26 27 96.3


line stmt bran cond sub pod time code
1             package Eve::Exception;
2              
3 31     31   5448 use strict;
  31         54  
  31         1035  
4 31     31   1583 use warnings;
  31         51  
  31         6198  
5              
6             =head1 NAME
7              
8             Eve::Exception - a module that defines a set of exception classes.
9              
10             =head1 SYNOPSIS
11              
12             To throw an exception:
13              
14             use Eve::Exception;
15              
16             Eve::Exception::Base->throw(message => 'Something bad happened.');
17              
18             To catch an exception:
19              
20             use Eve::Exception;
21              
22             eval {
23             # Code that throws an exception here
24             something();
25             };
26              
27             my $e;
28             if ($e = Eve::Exception::Data->caught()) {
29             # Do the thing
30             } elsif ($e = Eve::Exception::Http->caught()) {
31             # Do something else
32             } elsif ($e = Exception::Class::Base->caught()) {
33             $e->rethrow();
34             }
35              
36             It is conventional to catch Eve::Exception derivatives
37             only. Eve::Error derivatives are assumed to be uncatchable.
38              
39             =head1 DESCRIPTION
40              
41             =head2 Provided classes
42              
43             =over 4
44              
45             =item C
46              
47             =over 8
48              
49             =item C
50              
51             =item C
52              
53             =item C
54              
55             =item C
56              
57             =item C
58              
59             =item C
60              
61             =item C
62              
63             =back
64              
65             =item C
66              
67             =over 8
68              
69             =item C
70              
71             =item C
72              
73             =item C
74              
75             =item C
76              
77             =over 12
78              
79             =item C
80              
81             =item C
82              
83             =item C
84              
85             =item C
86              
87             =item C
88              
89             =back
90              
91             =item C
92              
93             =item C
94              
95             =back
96              
97             =back
98              
99             =cut
100              
101             use Exception::Class (
102             # Do not use ->caught() on this subtree of exceptions as they
103             # assumed to be uncatchable
104 31         707 'Eve::Error::Base',
105              
106             'Eve::Error::Attribute' => {
107             isa => 'Eve::Error::Base'},
108              
109             'Eve::Error::HttpDispatcher' => {
110             isa => 'Eve::Error::Base'},
111              
112             'Eve::Error::NotImplemented' => {
113             isa => 'Eve::Error::Base'},
114              
115             'Eve::Error::Program' => {
116             isa => 'Eve::Error::Base'},
117              
118             'Eve::Error::Session' => {
119             isa => 'Eve::Error::Base'},
120              
121             'Eve::Error::Template' => {
122             isa => 'Eve::Error::Base'},
123              
124             'Eve::Error::Value' => {
125             isa => 'Eve::Error::Base'},
126              
127             # This assumed to be ->caught()
128             'Eve::Exception::Base',
129              
130             'Eve::Exception::Data' => {
131             isa => 'Eve::Exception::Base'},
132              
133             'Eve::Exception::Data::LanguageNotSet' => {
134             isa => 'Eve::Exception::Data'},
135              
136             'Eve::Exception::Data::TemplateTextNotFound' => {
137             isa => 'Eve::Exception::Data'},
138              
139             # Raises on die
140             'Eve::Exception::Die' => {
141             isa => 'Eve::Exception::Base'},
142              
143             'Eve::Exception::Duplicate' => {
144             isa => 'Eve::Exception::Base'},
145              
146             'Eve::Exception::Http' => {
147             isa => 'Eve::Exception::Base'},
148              
149             'Eve::Exception::Http::400BadRequest' => {
150             isa => 'Eve::Exception::Http'},
151              
152             'Eve::Exception::Http::401Unauthorized' => {
153             isa => 'Eve::Exception::Http'},
154              
155             'Eve::Exception::Http::403Forbidden' => {
156             isa => 'Eve::Exception::Http'},
157              
158             'Eve::Exception::Http::404NotFound' => {
159             isa => 'Eve::Exception::Http'},
160              
161             'Eve::Exception::Http::405MethodNotAllowed' => {
162             isa => 'Eve::Exception::Http'},
163              
164             'Eve::Exception::InputOutput' => {
165             isa => 'Eve::Exception::Base'},
166              
167             'Eve::Exception::Privilege' => {
168             isa => 'Eve::Exception::Base'},
169 31     31   37817 );
  31         366057  
170              
171             # Redefine __DIE__ signal handler so it throws Eve::Exception::Die
172             $SIG{__DIE__} = sub {
173             unless (UNIVERSAL::isa($_[0], 'Exception::Class::Base')) {
174             Eve::Exception::Die->throw(message => join('', @_));
175             }
176             };
177              
178             # Redefine standart exception messaging so it prints exception class
179             # name when no message specifien in throw()
180 31     31   438071 no warnings;
  31         76  
  31         3448  
181             *Exception::Class::Base::full_message = sub {
182 303     303   8086214 my $self = shift;
183              
184             return
185 303 50       2337 (ref $self ? ref $self : $self).
    100          
186             ($self->message() ? ': '.$self->message() : '');
187             };
188 31     31   227 use warnings;
  31         108  
  31         2960  
189              
190             # Turn the stack trace printing on and off where it is necessary
191             Eve::Exception::Base->Trace(0);
192             Eve::Exception::Die->Trace(1);
193             Eve::Error::Base->Trace(1);
194              
195             =head1 SEE ALSO
196              
197             =over 4
198              
199             =item L
200              
201             =back
202              
203             =head1 LICENSE AND COPYRIGHT
204              
205             Copyright 2012 Igor Zinovyev.
206              
207             This program is free software; you can redistribute it and/or modify it
208             under the terms of either: the GNU General Public License as published
209             by the Free Software Foundation; or the Artistic License.
210              
211             See http://dev.perl.org/licenses/ for more information.
212              
213              
214             =head1 AUTHOR
215              
216             =over 4
217              
218             =item L
219              
220             =back
221              
222             =cut
223              
224             1;