From: Florian Ragwitz Date: Fri, 26 Jun 2009 15:28:24 +0000 (+0000) Subject: Make Exception initialisation saner. X-Git-Tag: 5.80006~26 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=62ef37bfeea1b4e5ec03ad4053d3ed7816758844 Make Exception initialisation saner. --- diff --git a/lib/Catalyst/Exception.pm b/lib/Catalyst/Exception.pm index 6956f0e..c8d5547 100644 --- a/lib/Catalyst/Exception.pm +++ b/lib/Catalyst/Exception.pm @@ -35,8 +35,9 @@ Throws a fatal exception. =cut has message => ( - is => 'ro', - isa => 'Str', + is => 'ro', + isa => 'Str', + default => sub { $! || '' }, ); use overload @@ -48,15 +49,23 @@ sub as_string { return $self->message; } -sub throw { - my $class = shift; - my %params = @_ == 1 ? ( error => $_[0] ) : @_; +around BUILDARGS => sub { + my ($next, $class, @args) = @_; + if (@args == 1 && !ref $args[0]) { + @args = (message => $args[0]); + } - $params{message} = $params{message} || $params{error} || $! || ''; - my $error = $class->new(%params); + my $args = $class->$next(@args); + $args->{message} ||= $args->{error} + if exists $args->{error}; - local $Carp::CarpLevel = 1; + return $args; +}; +sub throw { + my $class = shift; + my $error = $class->new(@_); + local $Carp::CarpLevel = 1; croak $error; }