X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst.pm;h=968661754a8e1aead348ddcd87b0a17cd5269221;hb=0a8aefd7e7d4de162e2c73f7b898e54699a9d8e1;hp=892fc4e465882c6d203f4f6ed164898ceed7ead9;hpb=c4df830e10aac24eb8ea9f0cb454d887b1761aa3;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 892fc4e..9686617 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -127,7 +127,7 @@ __PACKAGE__->stats_class('Catalyst::Stats'); __PACKAGE__->_encode_check(Encode::FB_CROAK | Encode::LEAVE_SRC); # Remember to update this in Catalyst::Runtime as well! -our $VERSION = '5.90077'; +our $VERSION = '5.90078'; sub import { my ( $class, @arguments ) = @_; @@ -518,6 +518,9 @@ Add a new error. $c->error('Something bad happened'); +Calling this will always return an arrayref (if there are no errors it +will be an empty arrayref. + =cut sub error { @@ -562,6 +565,29 @@ Returns true if you have errors sub has_errors { scalar(@{shift->error}) ? 1:0 } +=head2 $c->last_error + +Returns the most recent error in the stack (the one most recently added...) +or nothing if there are no errors. + +=cut + +sub last_error { my ($err, @errs) = @{shift->error}; return $err } + +=head2 shift_errors + +shifts the most recently added error off the error stack and returns if. Returns +nothing if there are nomore errors. + +=cut + +sub shift_errors { + my ($self) = @_; + my ($err, @errors) = @{$self->error}; + $self->{error} = \@errors; + return $err; +} + sub _comp_search_prefixes { my $c = shift; return map $c->components->{ $_ }, $c->_comp_names_search_prefixes(@_); @@ -1775,16 +1801,7 @@ sub execute { if ( my $error = $@ ) { #rethow if this can be handled by middleware - if( - !$c->config->{always_catch_http_exceptions} && - blessed $error && ( - $error->can('as_psgi') || - ( - $error->can('code') && - $error->code =~m/^[1-5][0-9][0-9]$/ - ) - ) - ) { + if ( $c->_handle_http_exception($error) ) { foreach my $err (@{$c->error}) { $c->log->error($err); } @@ -1965,11 +1982,7 @@ sub finalize_error { $c->engine->finalize_error( $c, @_ ); } else { my ($error) = @{$c->error}; - if( - !$c->config->{always_catch_http_exceptions} && - blessed $error && - ($error->can('as_psgi') || $error->can('code')) - ) { + if ( $c->_handle_http_exception($error) ) { # In the case where the error 'knows what it wants', becauses its PSGI # aware, just rethow and let middleware catch it $error->can('rethrow') ? $error->rethrow : croak $error; @@ -2111,16 +2124,7 @@ sub handle_request { $status = $c->finalize; } catch { #rethow if this can be handled by middleware - if( - !$class->config->{always_catch_http_exceptions} && - blessed($_) && ( - $_->can('as_psgi') || - ( - $_->can('code') && - $_->code =~m/^[1-5][0-9][0-9]$/ - ) - ) - ) { + if ( $class->_handle_http_exception($_) ) { $_->can('rethrow') ? $_->rethrow : croak $_; } chomp(my $error = $_); @@ -3437,12 +3441,34 @@ sub default_data_handlers { ->can('build_cgi_struct')->($params); }, 'application/json' => sub { - Class::Load::load_first_existing_class('JSON::MaybeXS', 'JSON') - ->can('decode_json')->(do { local $/; $_->getline }); - }, + my ($fh, $req) = @_; + my $parser = Class::Load::load_first_existing_class('JSON::MaybeXS', 'JSON'); + my $slurped; + return eval { + local $/; + $slurped = $fh->getline; + $parser->can("decode_json")->($slurped); + } || Catalyst::Exception->throw(sprintf "Error Parsing POST '%s', Error: %s", (defined($slurped) ? $slurped : 'undef') ,$@); + }, }; } +sub _handle_http_exception { + my ( $self, $error ) = @_; + if ( + !$self->config->{always_catch_http_exceptions} + && blessed $error + && ( + $error->can('as_psgi') + || ( $error->can('code') + && $error->code =~ m/^[1-5][0-9][0-9]$/ ) + ) + ) + { + return 1; + } +} + =head2 $c->stack Returns an arrayref of the internal execution stack (actions that are