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