6530a6ff41c1201c37a51cde450107937073fd8d
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Exception / Basic.pm
1 package Catalyst::Exception::Basic;
2
3 use MooseX::Role::WithOverloading;
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 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;