removed CE::PSGI from author deps testing
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Plugin / Unicode / Encoding.pm
index 544d46b..022efd2 100644 (file)
@@ -23,6 +23,10 @@ sub encoding {
         if (my $wanted = shift)  {
             $encoding = Encode::find_encoding($wanted)
               or Carp::croak( qq/Unknown encoding '$wanted'/ );
+            binmode(STDERR, ':encoding(' . $encoding->name . ')');
+        }
+        else {
+            binmode(STDERR);
         }
 
         $encoding = ref $c
@@ -81,6 +85,7 @@ sub prepare_uploads {
     $c->next::method(@_);
 
     my $enc = $c->encoding;
+    return unless $enc;
 
     for my $key (qw/ parameters query_parameters body_parameters /) {
         for my $value ( values %{ $c->request->{$key} } ) {
@@ -95,15 +100,17 @@ sub prepare_uploads {
     for my $value ( values %{ $c->request->uploads } ) {
         # 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 );
+        for my $inner_value ( ref($value) eq 'ARRAY' ? @{$value} : $value ) {
+            $inner_value->{filename} = try {
+                $enc->decode( $inner_value->{filename}, $CHECK )
+            } catch {
+                $c->handle_unicode_encoding_exception({
+                    param_value => $inner_value->{filename},
+                    error_msg => $_,
+                    encoding_step => 'uploads',
+                });
+            };
+        }
     }
 }
 
@@ -112,6 +119,9 @@ sub prepare_action {
 
     my $ret = $c->next::method(@_);
 
+    my $enc = $c->encoding;
+    return $ret unless $enc;
+
     foreach (@{$c->req->arguments}, @{$c->req->captures}) {
       $_ = $c->_handle_param_unicode_decoding($_);
     }
@@ -124,12 +134,12 @@ sub setup {
 
     my $conf = $self->config;
 
-    # Allow an explict undef encoding to disable default of utf-8
+    # Allow an explicit undef encoding to disable default of utf-8
     my $enc = delete $conf->{encoding};
     $self->encoding( $enc );
 
     return $self->next::method(@_)
-      unless $self->setup_finished; ## hack to stop possibily meaningless test fail... (jnap)
+      unless $self->setup_finished; ## hack to stop possibly meaningless test fail... (jnap)
 }
 
 sub _handle_unicode_decoding {
@@ -173,8 +183,7 @@ sub _handle_param_unicode_decoding {
 
 sub handle_unicode_encoding_exception {
     my ( $self, $exception_ctx ) = @_;
-    $self->log->warn($exception_ctx->{error_msg});
-    return $exception_ctx->{'param_value'};
+    die $exception_ctx->{error_msg};
 }
 
 1;
@@ -196,7 +205,7 @@ Catalyst::Plugin::Unicode::Encoding - Unicode aware Catalyst
 
 This plugin is automatically loaded by apps. Even though is not a core component
 yet, it will vanish as soon as the code is fully integrated. For more
-information, please refer to C<ENCODING> section at L<Catalyst>.
+information, please refer to L<Catalyst/ENCODING>.
 
 =head1 AUTHORS