Make public response building methods for errors
Wallace Reis [Fri, 1 Jun 2012 12:33:00 +0000 (14:33 +0200)]
_unsupported_media_type and _serialize_bad_request which can be extended
from an action-role to return custom error reponse.

lib/Catalyst/Action/Deserialize.pm
lib/Catalyst/Action/Serialize.pm
lib/Catalyst/Action/SerializeBase.pm

index ea03b9c..f978aac 100644 (file)
@@ -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<Catalyst::Request::REST>.
 
+=head1 CUSTOM ERRORS
+
+For building custom error responses when de-serialization fails, you can create
+an ActionRole (and use L<Catalyst::Controller::ActionRole> to apply it to the
+C<begin> action) which overrides C<unsupported_media_type> and/or C<_serialize_bad_request>
+methods.
+
 =head1 SEE ALSO
 
 You likely want to look at L<Catalyst::Controller::REST>, which implements
index 45eb227..3ed3d68 100644 (file)
@@ -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<Catalyst::Controller::ActionRole> to apply it to the
+C<end> action) which overrides C<unsupported_media_type> and/or C<_serialize_bad_request>
+methods.
+
 =head1 SEE ALSO
 
 You likely want to look at L<Catalyst::Controller::REST>, which implements
index a09f6dd..60ea05e 100644 (file)
@@ -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);