private_path method for actions that returns a string suitable for use in forward...
[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     default => sub { $! || '' },
41 );
42
43 use overload
44     q{""}    => \&as_string,
45     fallback => 1;
46
47 sub as_string {
48     my ($self) = @_;
49     return $self->message;
50 }
51
52 around BUILDARGS => sub {
53     my ($next, $class, @args) = @_;
54     if (@args == 1 && !ref $args[0]) {
55         @args = (message => $args[0]);
56     }
57
58     my $args = $class->$next(@args);
59     $args->{message} ||= $args->{error}
60         if exists $args->{error};
61
62     return $args;
63 };
64
65 sub throw {
66     my $class = shift;
67     my $error = $class->new(@_);
68     local $Carp::CarpLevel = 1;
69     croak $error;
70 }
71
72 sub rethrow {
73     my ($self) = @_;
74     croak $self;
75 }
76
77 =head2 meta
78
79 Provided by Moose
80
81 =head1 AUTHORS
82
83 Catalyst Contributors, see Catalyst.pm
84
85 =head1 COPYRIGHT
86
87 This library is free software. You can redistribute it and/or modify
88 it under the same terms as Perl itself.
89
90 =cut
91
92 Catalyst::Exception::Base->meta->make_immutable;
93
94 package Catalyst::Exception;
95
96 use Moose;
97 use namespace::clean -except => 'meta';
98
99 use vars qw[$CATALYST_EXCEPTION_CLASS];
100
101 BEGIN {
102     extends($CATALYST_EXCEPTION_CLASS || 'Catalyst::Exception::Base');
103 }
104
105 __PACKAGE__->meta->make_immutable;
106
107 1;