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