From: Wallace Reis Date: Fri, 1 Jun 2012 12:33:00 +0000 (+0200) Subject: Make public response building methods for errors X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-REST.git;a=commitdiff_plain;h=0af29a376773949b026ed4cbba221749a9279032 Make public response building methods for errors _unsupported_media_type and _serialize_bad_request which can be extended from an action-role to return custom error reponse. --- diff --git a/lib/Catalyst/Action/Deserialize.pm b/lib/Catalyst/Action/Deserialize.pm index ea03b9c..f978aac 100644 --- a/lib/Catalyst/Action/Deserialize.pm +++ b/lib/Catalyst/Action/Deserialize.pm @@ -47,9 +47,9 @@ sub execute { $rc = $sclass->execute( $controller, $c ); } if ( $rc eq "0" ) { - return $self->_unsupported_media_type( $c, $content_type ); + return $self->unsupported_media_type( $c, $content_type ); } elsif ( $rc ne "1" ) { - return $self->_serialize_bad_request( $c, $content_type, $rc ); + return $self->serialize_bad_request( $c, $content_type, $rc ); } } @@ -120,6 +120,13 @@ Will work just fine. When you use this module, the request class will be changed to L. +=head1 CUSTOM ERRORS + +For building custom error responses when de-serialization fails, you can create +an ActionRole (and use L to apply it to the +C action) which overrides C and/or C<_serialize_bad_request> +methods. + =head1 SEE ALSO You likely want to look at L, which implements diff --git a/lib/Catalyst/Action/Serialize.pm b/lib/Catalyst/Action/Serialize.pm index 45eb227..3ed3d68 100644 --- a/lib/Catalyst/Action/Serialize.pm +++ b/lib/Catalyst/Action/Serialize.pm @@ -54,9 +54,9 @@ sub execute { } }; if ($@) { - return $self->_serialize_bad_request( $c, $content_type, $@ ); + return $self->serialize_bad_request( $c, $content_type, $@ ); } elsif (!$rc) { - return $self->_unsupported_media_type( $c, $content_type ); + return $self->unsupported_media_type( $c, $content_type ); } return 1; @@ -140,6 +140,13 @@ Daisuke Maki pointed out that early versions of this Action did not play well with others, or generally behave in a way that was very consistent with the rest of Catalyst. +=head1 CUSTOM ERRORS + +For building custom error responses when serialization fails, you can create +an ActionRole (and use L to apply it to the +C action) which overrides C and/or C<_serialize_bad_request> +methods. + =head1 SEE ALSO You likely want to look at L, which implements diff --git a/lib/Catalyst/Action/SerializeBase.pm b/lib/Catalyst/Action/SerializeBase.pm index a09f6dd..60ea05e 100644 --- a/lib/Catalyst/Action/SerializeBase.pm +++ b/lib/Catalyst/Action/SerializeBase.pm @@ -75,7 +75,7 @@ sub _load_content_plugins { # pick the best match that we have a serializer mapping for my ($content_type) = grep { $map->{$_} } @accepted_types; - return $self->_unsupported_media_type($c, $content_type) + return $self->unsupported_media_type($c, $content_type) if not $content_type; # carp about old text/x-json @@ -101,10 +101,10 @@ sub _load_content_plugins { $sclass .= $mc; #} if ( !grep( /^$sclass$/, @{ $self->_serialize_plugins } ) ) { - return $self->_unsupported_media_type($c, $content_type); + return $self->unsupported_media_type($c, $content_type); } } else { - return $self->_unsupported_media_type($c, $content_type); + return $self->unsupported_media_type($c, $content_type); } unless ( exists( $self->_loaded_plugins->{$sclass} ) ) { my $load_class = $sclass; @@ -114,7 +114,7 @@ sub _load_content_plugins { if ($@) { $c->log->error( "Error loading $sclass for " . $content_type . ": $!" ); - return $self->_unsupported_media_type($c, $content_type); + return $self->unsupported_media_type($c, $content_type); } else { $self->_loaded_plugins->{$sclass} = 1; } @@ -132,7 +132,7 @@ sub _load_content_plugins { return $sclass, $sarg, $content_type; } -sub _unsupported_media_type { +sub unsupported_media_type { my ( $self, $c, $content_type ) = @_; $c->res->content_type('text/plain'); $c->res->status(415); @@ -146,7 +146,7 @@ sub _unsupported_media_type { return undef; } -sub _serialize_bad_request { +sub serialize_bad_request { my ( $self, $c, $content_type, $error ) = @_; $c->res->content_type('text/plain'); $c->res->status(400);