[Catalyst-commits] r14095 - in Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback: . lib/Catalyst/Plugin/Unicode

t0m at dev.catalyst.perl.org t0m at dev.catalyst.perl.org
Wed Sep 7 20:08:24 GMT 2011


Author: t0m
Date: 2011-09-07 20:08:24 +0000 (Wed, 07 Sep 2011)
New Revision: 14095

Modified:
   Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback/README
   Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback/lib/Catalyst/Plugin/Unicode/Encoding.pm
Log:
Simplify, and go back to exception by default

Modified: Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback/README
===================================================================
--- Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback/README	2011-09-07 18:27:53 UTC (rev 14094)
+++ Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback/README	2011-09-07 20:08:24 UTC (rev 14095)
@@ -32,6 +32,30 @@
         Setups "$c->encoding" with encoding specified in
         "$c->config->{encoding}".
 
+    handle_unicode_encoding_exception ($exception_context)
+        Method called when decoding process for a request fails.
+
+        An $exception_context hashref is provided to allow you to override
+        the behaviour of your application when given data with incorrect
+        encodings.
+
+        The default method throws exceptions in the case of invalid request
+        parameters (resulting in a 500 error), but ignores errors in upload
+        filenames.
+
+        The keys passed in the $exception_context hash are:
+
+        param_value
+            The value which was not able to be decoded.
+
+        error_msg
+            The exception recieved from Encode.
+
+        encoding_step
+            What type of data was being decoded. Valid values are
+            (currently) "params" - for request parameters / arguments /
+            captures and "uploads" - for request upload filenames.
+
 SEE ALSO
     Encode, Encode::Encoding, Catalyst::Plugin::Unicode, Catalyst.
 

Modified: Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback/lib/Catalyst/Plugin/Unicode/Encoding.pm
===================================================================
--- Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback/lib/Catalyst/Plugin/Unicode/Encoding.pm	2011-09-07 18:27:53 UTC (rev 14094)
+++ Catalyst-Plugin-Unicode-Encoding/branches/decoding_callback/lib/Catalyst/Plugin/Unicode/Encoding.pm	2011-09-07 20:08:24 UTC (rev 14095)
@@ -95,7 +95,7 @@
                 #      this avoids exception if we have already decoded content, and is _not_ the
                 #      same as not encoding on output which is bad news (as it does the wrong thing
                 #      for latin1 chars for example)..
-	        $_ = $c->_handle_param_unicode_decoding($_);
+                $_ = $c->_handle_param_unicode_decoding($_);
             }
         }
     }
@@ -103,14 +103,14 @@
         # skip if it fails for uploads, as we don't usually want uploads touched
         # in any way
         $_->{filename} = try {
-	    $enc->decode( $_->{filename}, $CHECK )
-	} catch {
-	    $c->handle_unicode_encoding_exception({
-	        param_value => $_->{filename},
-		error_msg => $_,
-		encoding_step => 'uploads',
-	    });
-	} for ( ref($value) eq 'ARRAY' ? @{$value} : $value );
+        $enc->decode( $_->{filename}, $CHECK )
+    } catch {
+        $c->handle_unicode_encoding_exception({
+            param_value => $_->{filename},
+            error_msg => $_,
+            encoding_step => 'uploads',
+        });
+    } for ( ref($value) eq 'ARRAY' ? @{$value} : $value );
     }
 }
 
@@ -144,31 +144,20 @@
     return try {
         Encode::is_utf8( $value ) ?
             $value
-	    : $enc->decode( $value, $CHECK );
-    } catch {
+        : $enc->decode( $value, $CHECK );
+    }
+    catch {
         $self->handle_unicode_encoding_exception({
-	  param_value => $value,
-	  error_msg => $_,
-	  encoding_step => 'params',
-	});
+            param_value => $value,
+            error_msg => $_,
+            encoding_step => 'params',
+        });
     };
 }
 
 sub handle_unicode_encoding_exception {
     my ( $self, $exception_ctx ) = @_;
-    my ( $param_value, $error_msg, $encoding_step )
-        = @$exception_ctx{qw(param_value error_msg encoding_step)};
-    my $fallback_action_map = {
-      params => sub { die pop },
-      uploads => sub { shift; return shift }
-    };
-
-    if ( my $do_action = $fallback_action_map->{$encoding_step} ) {
-      return $do_action->($param_value, $error_msg);
-    }
-    else {
-      die $error_msg;
-    }
+    die $exception_ctx->{error_msg};
 }
 
 1;
@@ -225,12 +214,38 @@
 
 Setups C<< $c->encoding >> with encoding specified in C<< $c->config->{encoding} >>.
 
-=item handle_unicode_encoding_exception
+=item handle_unicode_encoding_exception ($exception_context)
 
-Callback method used for failed decoding process.
+Method called when decoding process for a request fails.
 
+An C<$exception_context> hashref is provided to allow you to override the
+behaviour of your application when given data with incorrect encodings.
+
+The default method throws exceptions in the case of invalid request parameters
+(resulting in a 500 error), but ignores errors in upload filenames.
+
+The keys passed in the C<$exception_context> hash are:
+
+=over
+
+=item param_value
+
+The value which was not able to be decoded.
+
+=item error_msg
+
+The exception recieved from L<Encode>.
+
+=item encoding_step
+
+What type of data was being decoded. Valid values are (currently)
+C<params> - for request parameters / arguments / captures
+and C<uploads> - for request upload filenames.
+
 =back
 
+=back
+
 =head1 SEE ALSO
 
 L<Encode>, L<Encode::Encoding>, L<Catalyst::Plugin::Unicode>, L<Catalyst>.




More information about the Catalyst-commits mailing list