do not require MXRWO if Moose is new enough to have cored it
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Exception / Basic.pm
CommitLineData
ede5238f 1package Catalyst::Exception::Basic;
2
2aba16c2 3use Moose::Role;
4use if !eval { require Moose; Moose->VERSION('2.1300') },
5 'MooseX::Role::WithOverloading';
ede5238f 6use Carp;
7use namespace::clean -except => 'meta';
8
9with 'Catalyst::Exception::Interface';
10
11has message => (
12 is => 'ro',
13 isa => 'Str',
14 default => sub { $! || '' },
15);
16
ede5238f 17sub as_string {
18 my ($self) = @_;
19 return $self->message;
20}
21
22around BUILDARGS => sub {
23 my ($next, $class, @args) = @_;
24 if (@args == 1 && !ref $args[0]) {
25 @args = (message => $args[0]);
26 }
27
28 my $args = $class->$next(@args);
29 $args->{message} ||= $args->{error}
30 if exists $args->{error};
31
32 return $args;
33};
34
35sub throw {
36 my $class = shift;
37 my $error = $class->new(@_);
38 local $Carp::CarpLevel = 1;
39 croak $error;
40}
41
42sub rethrow {
43 my ($self) = @_;
44 croak $self;
45}
46
471;
b6c6af73 48
49=head1 NAME
50
51Catalyst::Exception::Basic - Basic Catalyst Exception Role
52
53=head1 SYNOPSIS
54
55 package My::Exception;
56 use Moose;
57 use namespace::clean -except => 'meta';
a62b43cd 58
b6c6af73 59 with 'Catalyst::Exception::Basic';
a62b43cd 60
b6c6af73 61 # Elsewhere..
a62b43cd 62 My::Exception->throw( qq/Fatal exception/ );
b6c6af73 63
64See also L<Catalyst> and L<Catalyst::Exception>.
65
66=head1 DESCRIPTION
67
68This is the basic Catalyst Exception role which implements all of
69L<Catalyst::Exception::Interface>.
70
71=head1 ATTRIBUTES
72
73=head2 message
74
75Holds the exception message.
76
77=head1 METHODS
78
79=head2 as_string
80
a62b43cd 81Stringifies the exception's message attribute.
b6c6af73 82Called when the object is stringified by overloading.
83
84=head2 throw( $message )
85
86=head2 throw( message => $message )
87
88=head2 throw( error => $error )
89
90Throws a fatal exception.
91
92=head2 rethrow( $exception )
93
94Rethrows a caught exception.
95
96=head2 meta
97
98Provided by Moose
99
100=head1 AUTHORS
101
102Catalyst Contributors, see Catalyst.pm
103
104=head1 COPYRIGHT
105
106This library is free software. You can redistribute it and/or modify
107it under the same terms as Perl itself.
108
109=cut