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