Skip to content

Instantly share code, notes, and snippets.

@nihen
Created January 7, 2010 17:19
Show Gist options
  • Save nihen/271381 to your computer and use it in GitHub Desktop.
Save nihen/271381 to your computer and use it in GitHub Desktop.
diff --git a/lib/Plack/Middleware/StackTrace.pm b/lib/Plack/Middleware/StackTrace.pm
index bee07e2..26b09c0 100644
--- a/lib/Plack/Middleware/StackTrace.pm
+++ b/lib/Plack/Middleware/StackTrace.pm
@@ -7,6 +7,7 @@ use Plack::Util;
use Devel::StackTrace;
use Devel::StackTrace::AsHTML;
use Try::Tiny;
+use Encode;
our $StackTraceClass = "Devel::StackTrace";
@@ -28,6 +29,7 @@ sub call {
if ($trace && (!$res or $res->[0] == 500)) {
my $body = $trace->as_html;
+ $body = Encode::encode('utf-8', $body) if Encode::is_utf8($body);
$res = [500, ['Content-Type' => 'text/html; charset=utf-8'], [ $body ]];
}
diff --git a/t/Plack-Middleware/stacktrace_utf8.t b/t/Plack-Middleware/stacktrace_utf8.t
new file mode 100644
index 0000000..7e9755c
--- /dev/null
+++ b/t/Plack-Middleware/stacktrace_utf8.t
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use utf8;
+use Test::More;
+use Plack::Middleware::StackTrace;
+use Encode;
+
+my $handler = Plack::Middleware::StackTrace->wrap(sub { die 'あああ' });
+my $res = $handler->(+{});
+is scalar(@$res), 3;
+is $res->[0], 500;
+is_deeply $res->[1], ['Content-Type' => 'text/html; charset=utf-8'];
+like Encode::decode('utf-8', $res->[2]->[0]), qr/あああ/;
+ok !Encode::is_utf8($res->[2][0]);
+
+done_testing;
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment