Skip to content

Commit

Permalink
Merge branch 'master' into middleware-api2
Browse files Browse the repository at this point in the history
Conflicts:
	scripts/plackup
	t/Plack-Middleware/order.t
  • Loading branch information
miyagawa committed Oct 9, 2009
2 parents 4a1e2a1 + 850bc4f commit cb2c24a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 21 deletions.
7 changes: 6 additions & 1 deletion Makefile.PL
Expand Up @@ -30,7 +30,12 @@ feature 'Serve static files with sendfile(2)',
'Sys::Sendfile' => 0.09,
'IO::AIO' => 3.3;

requires 'CGI::ExceptionManager'; # Middleware::StackTrace
requires 'Devel::StackTrace'; # Middleware::StackTrace
requires 'Devel::StackTrace::AsHTML'; # Middleware::StackTrace

feature 'Stacktrace with lexical variables',
'Devel::StackTrace::WithLexicals';

requires 'Path::Class'; # Middleware::Static
requires 'MIME::Types'; # Middleware::Static

Expand Down
5 changes: 4 additions & 1 deletion eg/dot-psgi/error.psgi
@@ -1 +1,4 @@
sub { die "Oops" };
sub {
my $x = "bar"; my $y = [1,2,3]; my $z = { x => 1 }; my @y = qw(foo bar); my %z = (x => 1, y => 2);
die "Oops";
};
33 changes: 19 additions & 14 deletions lib/Plack/Middleware/StackTrace.pm
Expand Up @@ -3,32 +3,35 @@ use strict;
use warnings;
use base qw/Plack::Middleware/;
use Plack;
use CGI::ExceptionManager::StackTrace;
use Encode;
use Plack::Util;
use Data::Dump;
use Devel::StackTrace;
use Devel::StackTrace::AsHTML;

__PACKAGE__->mk_accessors(qw/renderer/);
our $StackTraceClass = "Devel::StackTrace";

# Optional since it needs PadWalker
if (eval { require Devel::StackTrace::WithLexicals; 1 }) {
$StackTraceClass = "Devel::StackTrace::WithLexicals";
}

sub call {
my($self, $env) = @_;

my $err_info;
my $trace;
local $SIG{__DIE__} = sub {
my($msg) = @_;
$err_info = CGI::ExceptionManager::StackTrace->new($msg);
die $msg;
$trace = $StackTraceClass->new;
die @_;
};

my $res = do {
local $@;
eval { $self->app->($env) };
};

if ($err_info) {
my $body = $err_info->as_html(
powered_by => "Plack/$Plack::VERSION",
renderer => $self->renderer,
);
$res = [500, ['Content-Type' => 'text/html; charset=utf-8'], [ encode_utf8($body) ]];
if (!$res && $trace) {
my $body = $trace->as_html;
$res = [500, ['Content-Type' => 'text/html; charset=utf-8'], [ $body ]];
}

return $res;
Expand Down Expand Up @@ -62,9 +65,11 @@ No configuration option is available.
Tokuhiro Matsuno
Tatsuhiko Miyagawa
=head1 SEE ALSO
L<CGI::ExceptionManager> L<Plack::Middleware>
L<Devel::StackTrace::AsHTML> L<Plack::Middleware>
=cut
10 changes: 10 additions & 0 deletions lib/Plack/Util.pm
Expand Up @@ -196,6 +196,16 @@ sub status_with_no_entity_body {
return $status < 200 || $status == 204 || $status == 304;
}

sub encode_html {
my $str = shift;
$str =~ s/&/&amp;/g;
$str =~ s/>/&gt;/g;
$str =~ s/</&lt;/g;
$str =~ s/"/&quot;/g;
$str =~ s/'/&#39;/g;
return $str;
}

sub inline_object {
my %args = @_;
bless {%args}, 'Plack::Util::Prototype';
Expand Down
5 changes: 2 additions & 3 deletions scripts/plackup
Expand Up @@ -31,10 +31,9 @@ lib->import(@includes) if @includes;
my $handler = Plack::Util::load_psgi $app;

if ($env eq 'development') {
if (eval "require Plack::Middleware::StackTrace; 1") {
$handler = Plack::Middleware::StackTrace->wrap($handler);
}
require Plack::Middleware::StackTrace;
require Plack::Middleware::AccessLog;
$handler = Plack::Middleware::StackTrace->wrap($handler);
$handler = Plack::Middleware::AccessLog->wrap($handler, logger => sub { print STDERR @_ });
}

Expand Down
1 change: 0 additions & 1 deletion t/Plack-Middleware/order.t
@@ -1,5 +1,4 @@
use strict;
use Test::Requires qw( CGI::ExceptionManager );
use Plack::Builder;
use Test::More;

Expand Down
1 change: 0 additions & 1 deletion t/Plack-Middleware/stacktrace.t
@@ -1,7 +1,6 @@
use strict;
use warnings;
use Test::More;
use Test::Requires qw( CGI::ExceptionManager );
use Plack::Middleware::StackTrace;

my $handler = Plack::Middleware::StackTrace->wrap(sub { die "orz" });
Expand Down

0 comments on commit cb2c24a

Please sign in to comment.