Add Exception::Interface and Exception::Basic. Make ::Base, ::Go and ::Detach use...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Exception / Basic.pm
1 package Catalyst::Exception::Basic;
2
3 use Moose::Role;
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
15 use overload
16     q{""}    => \&as_string,
17     fallback => 1;
18
19 sub as_string {
20     my ($self) = @_;
21     return $self->message;
22 }
23
24 around BUILDARGS => sub {
25     my ($next, $class, @args) = @_;
26     if (@args == 1 && !ref $args[0]) {
27         @args = (message => $args[0]);
28     }
29
30     my $args = $class->$next(@args);
31     $args->{message} ||= $args->{error}
32         if exists $args->{error};
33
34     return $args;
35 };
36
37 sub throw {
38     my $class = shift;
39     my $error = $class->new(@_);
40     local $Carp::CarpLevel = 1;
41     croak $error;
42 }
43
44 sub rethrow {
45     my ($self) = @_;
46     croak $self;
47 }
48
49 1;