Commit | Line | Data |
ede5238f |
1 | package Catalyst::Exception::Basic; |
2 | |
80e287ba |
3 | use MooseX::Role::WithOverloading; |
ede5238f |
4 | use Carp; |
5 | use namespace::clean -except => 'meta'; |
6 | |
7 | with 'Catalyst::Exception::Interface'; |
8 | |
9 | has message => ( |
10 | is => 'ro', |
11 | isa => 'Str', |
12 | default => sub { $! || '' }, |
13 | ); |
14 | |
ede5238f |
15 | sub as_string { |
16 | my ($self) = @_; |
17 | return $self->message; |
18 | } |
19 | |
20 | around 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 | |
33 | sub throw { |
34 | my $class = shift; |
35 | my $error = $class->new(@_); |
36 | local $Carp::CarpLevel = 1; |
37 | croak $error; |
38 | } |
39 | |
40 | sub rethrow { |
41 | my ($self) = @_; |
42 | croak $self; |
43 | } |
44 | |
45 | 1; |
b6c6af73 |
46 | |
47 | =head1 NAME |
48 | |
49 | Catalyst::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 | |
62 | See also L<Catalyst> and L<Catalyst::Exception>. |
63 | |
64 | =head1 DESCRIPTION |
65 | |
66 | This is the basic Catalyst Exception role which implements all of |
67 | L<Catalyst::Exception::Interface>. |
68 | |
69 | =head1 ATTRIBUTES |
70 | |
71 | =head2 message |
72 | |
73 | Holds the exception message. |
74 | |
75 | =head1 METHODS |
76 | |
77 | =head2 as_string |
78 | |
a62b43cd |
79 | Stringifies the exception's message attribute. |
b6c6af73 |
80 | Called 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 | |
88 | Throws a fatal exception. |
89 | |
90 | =head2 rethrow( $exception ) |
91 | |
92 | Rethrows a caught exception. |
93 | |
94 | =head2 meta |
95 | |
96 | Provided by Moose |
97 | |
98 | =head1 AUTHORS |
99 | |
100 | Catalyst Contributors, see Catalyst.pm |
101 | |
102 | =head1 COPYRIGHT |
103 | |
104 | This library is free software. You can redistribute it and/or modify |
105 | it under the same terms as Perl itself. |
106 | |
107 | =cut |