Commit | Line | Data |
---|---|---|
a2f2cde9 | 1 | package Catalyst::Exception; |
2 | ||
3cdcf968 | 3 | # XXX: See bottom of file for Exception implementation |
a2f2cde9 | 4 | |
5 | package Catalyst::Exception::Base; | |
6 | ||
3cdcf968 | 7 | use Moose; |
a80247cf | 8 | use Carp; |
9 | use namespace::clean -except => 'meta'; | |
a2f2cde9 | 10 | |
11 | =head1 NAME | |
12 | ||
13 | Catalyst::Exception - Catalyst Exception Class | |
14 | ||
15 | =head1 SYNOPSIS | |
16 | ||
17 | Catalyst::Exception->throw( qq/Fatal exception/ ); | |
18 | ||
19 | See also L<Catalyst>. | |
20 | ||
21 | =head1 DESCRIPTION | |
22 | ||
23 | This is the Catalyst Exception class. | |
24 | ||
25 | =head1 METHODS | |
26 | ||
b5ecfcf0 | 27 | =head2 throw( $message ) |
a2f2cde9 | 28 | |
b5ecfcf0 | 29 | =head2 throw( message => $message ) |
ebfde331 | 30 | |
b5ecfcf0 | 31 | =head2 throw( error => $error ) |
a2f2cde9 | 32 | |
33 | Throws a fatal exception. | |
34 | ||
35 | =cut | |
36 | ||
9ffa230b | 37 | has message => ( |
62ef37bf | 38 | is => 'ro', |
39 | isa => 'Str', | |
40 | default => sub { $! || '' }, | |
9ffa230b | 41 | ); |
42 | ||
ccf2a6da | 43 | use overload |
44 | q{""} => \&as_string, | |
45 | fallback => 1; | |
0af1b40c | 46 | |
47 | sub as_string { | |
48 | my ($self) = @_; | |
49 | return $self->message; | |
50 | } | |
51 | ||
62ef37bf | 52 | around BUILDARGS => sub { |
53 | my ($next, $class, @args) = @_; | |
54 | if (@args == 1 && !ref $args[0]) { | |
55 | @args = (message => $args[0]); | |
56 | } | |
a2f2cde9 | 57 | |
62ef37bf | 58 | my $args = $class->$next(@args); |
59 | $args->{message} ||= $args->{error} | |
60 | if exists $args->{error}; | |
a2f2cde9 | 61 | |
62ef37bf | 62 | return $args; |
63 | }; | |
a2f2cde9 | 64 | |
62ef37bf | 65 | sub throw { |
66 | my $class = shift; | |
67 | my $error = $class->new(@_); | |
68 | local $Carp::CarpLevel = 1; | |
44b74e6a | 69 | croak $error; |
70 | } | |
71 | ||
72 | sub rethrow { | |
73 | my ($self) = @_; | |
74 | croak $self; | |
a2f2cde9 | 75 | } |
76 | ||
3cdcf968 | 77 | =head2 meta |
78 | ||
79 | Provided by Moose | |
80 | ||
2f381252 | 81 | =head1 AUTHORS |
a2f2cde9 | 82 | |
2f381252 | 83 | Catalyst Contributors, see Catalyst.pm |
a2f2cde9 | 84 | |
85 | =head1 COPYRIGHT | |
86 | ||
536bee89 | 87 | This library is free software. You can redistribute it and/or modify |
a2f2cde9 | 88 | it under the same terms as Perl itself. |
89 | ||
90 | =cut | |
91 | ||
e5ecd5bc | 92 | Catalyst::Exception::Base->meta->make_immutable; |
93 | ||
3cdcf968 | 94 | package Catalyst::Exception; |
95 | ||
96 | use Moose; | |
a80247cf | 97 | use namespace::clean -except => 'meta'; |
98 | ||
3cdcf968 | 99 | use vars qw[$CATALYST_EXCEPTION_CLASS]; |
100 | ||
101 | BEGIN { | |
102 | extends($CATALYST_EXCEPTION_CLASS || 'Catalyst::Exception::Base'); | |
103 | } | |
104 | ||
6680c772 | 105 | __PACKAGE__->meta->make_immutable; |
e5ecd5bc | 106 | |
a2f2cde9 | 107 | 1; |