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