| 1 | package Eve::Exception; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 32 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 32 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 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<Eve::Error::Base> | ||||||||||||||
| 46 | |||||||||||||||
| 47 | =over 8 | ||||||||||||||
| 48 | |||||||||||||||
| 49 | =item C<Eve::Error::Attribute> | ||||||||||||||
| 50 | |||||||||||||||
| 51 | =item C<Eve::Error::HttpDispatcher> | ||||||||||||||
| 52 | |||||||||||||||
| 53 | =item C<Eve::Error::NotImplemented> | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =item C<Eve::Error::Program> | ||||||||||||||
| 56 | |||||||||||||||
| 57 | =item C<Eve::Error::Session> | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =item C<Eve::Error::Template> | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =item C<Eve::Error::Value> | ||||||||||||||
| 62 | |||||||||||||||
| 63 | =back | ||||||||||||||
| 64 | |||||||||||||||
| 65 | =item C<Eve::Exception::Base> | ||||||||||||||
| 66 | |||||||||||||||
| 67 | =over 8 | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =item C<Eve::Exception::Data> | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =item C<Eve::Exception::Die> | ||||||||||||||
| 72 | |||||||||||||||
| 73 | =item C<Eve::Exception::Duplicate> | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =item C<Eve::Exception::Http> | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =over 12 | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =item C<Eve::Exception::Http::400BadRequest> | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =item C<Eve::Exception::Http::401Unauthorized> | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =item C<Eve::Exception::Http::404NotFound> | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =item C<Eve::Exception::Http::403Forbidden> | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =item C<Eve::Exception::Http::405MethodNotAllowed> | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =back | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =item C<Eve::Exception::InputOutput> | ||||||||||||||
| 92 | |||||||||||||||
| 93 | =item C<Eve::Exception::Privilege> | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =back | ||||||||||||||
| 96 | |||||||||||||||
| 97 | =back | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =cut | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 32 | ▶ | use Exception::Class ( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 102 | # Do not use ->caught() on this subtree of exceptions as they | ||||||||||||||
| 103 | # assumed to be uncatchable | ||||||||||||||
| 104 | 32 | '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 | 32 | ); | |||||||||||||
| 170 | |||||||||||||||
| 171 | # Redefine __DIE__ signal handler so it throws Eve::Exception::Die | ||||||||||||||
| 172 | 292 | ▶ | $SIG{__DIE__} = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 173 | 292 | ▶ | unless (UNIVERSAL::isa($_[0], 'Exception::Class::Base')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | 257 | Eve::Exception::Die->throw(message => join('', @_)); | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | 32 | }; | |||||||||||||
| 177 | |||||||||||||||
| 178 | # Redefine standart exception messaging so it prints exception class | ||||||||||||||
| 179 | # name when no message specifien in throw() | ||||||||||||||
| 180 | 32 | ▶ | no warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 181 | 314 | ▶ | *Exception::Class::Base::full_message = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||
| 182 | 314 | my $self = shift; | |||||||||||||
| 183 | |||||||||||||||
| 184 | return | ||||||||||||||
| 185 | 314 | ▶ | (ref $self ? ref $self : $self). | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 186 | ($self->message() ? ': '.$self->message() : ''); | ||||||||||||||
| 187 | 32 | }; | |||||||||||||
| 188 | 32 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 189 | |||||||||||||||
| 190 | # Turn the stack trace printing on and off where it is necessary | ||||||||||||||
| 191 | 32 | Eve::Exception::Base->Trace(0); | |||||||||||||
| 192 | 32 | Eve::Exception::Die->Trace(1); | |||||||||||||
| 193 | 32 | Eve::Error::Base->Trace(1); | |||||||||||||
| 194 | |||||||||||||||
| 195 | =head1 SEE ALSO | ||||||||||||||
| 196 | |||||||||||||||
| 197 | =over 4 | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =item L<Exception::Class> | ||||||||||||||
| 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<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||||||||||||||
| 219 | |||||||||||||||
| 220 | =back | ||||||||||||||
| 221 | |||||||||||||||
| 222 | =cut | ||||||||||||||
| 223 | |||||||||||||||
| 224 | 32 | 1; | |||||||||||||