Make Catalyst::Exception throw exception instances instead of plain strings.
[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 q{""} => \&as_string;
43
44 sub as_string {
45     my ($self) = @_;
46     return $self->message;
47 }
48
49 sub throw {
50     my $class  = shift;
51     my %params = @_ == 1 ? ( error => $_[0] ) : @_;
52
53     $params{message} = $params{message} || $params{error} || $! || '';
54     my $error = $class->new(%params);
55
56     local $Carp::CarpLevel = 1;
57
58     croak($error);
59 }
60
61 =head2 meta
62
63 Provided by Moose
64
65 =head1 AUTHORS
66
67 Catalyst Contributors, see Catalyst.pm
68
69 =head1 COPYRIGHT
70
71 This library is free software. You can redistribute it and/or modify
72 it under the same terms as Perl itself.
73
74 =cut
75
76 Catalyst::Exception::Base->meta->make_immutable;
77
78 package Catalyst::Exception;
79
80 use Moose;
81 use namespace::clean -except => 'meta';
82
83 use vars qw[$CATALYST_EXCEPTION_CLASS];
84
85 BEGIN {
86     extends($CATALYST_EXCEPTION_CLASS || 'Catalyst::Exception::Base');
87 }
88
89 __PACKAGE__->meta->make_immutable;
90
91 1;