Merge branch 'exception_interface'
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Exception / Basic.pm
CommitLineData
ede5238f 1package Catalyst::Exception::Basic;
2
80e287ba 3use MooseX::Role::WithOverloading;
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;