Skip to content

Commit

Permalink
added Plack::Util::wrap_error so servers can use to catch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Sep 17, 2009
1 parent 44528b7 commit 4154a68
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/Perlbal/Plugin/PSGI.pm
Expand Up @@ -60,7 +60,7 @@ sub handle_request {
);
};

my $res = $app->($env, $start_response);
my $res = Plack::Util::wrap_error { $app->($env, $start_response) } $env;
return 1 if @$res == 0;

$start_response->($res);
Expand Down
2 changes: 1 addition & 1 deletion lib/Plack/Impl/AnyEvent.pm
Expand Up @@ -137,7 +137,7 @@ sub _response_handler {

return sub {
my($app, $env) = @_;
my $res = $app->($env, $start_response);
my $res = Plack::Util::wrap_error { $app->($env, $start_response) } $env;
return if scalar(@$res) == 0;

$start_response->($res->[0], $res->[1]);
Expand Down
2 changes: 1 addition & 1 deletion lib/Plack/Impl/Coro.pm
Expand Up @@ -54,7 +54,7 @@ sub process_request {

my $reqlen = parse_http_request($buf, $env);
if ($reqlen >= 0) {
$res = $self->{app}->($env);
$res = Plack::Util::wrap_error { $self->{app}->($env) } $env;
last;
} elsif ($reqlen == -2) {
# incomplete, continue
Expand Down
2 changes: 1 addition & 1 deletion lib/Plack/Impl/Danga/Socket.pm
Expand Up @@ -198,7 +198,7 @@ sub _response_handler {
Scalar::Util::weaken($socket);
return sub {
my ($app, $env) = @_;
my $res = $app->($env, $state_response);
my $res = Plack::Util::wrap_error { $app->($env, $state_response) } $env;
return if scalar(@$res) == 0;

$state_response->($res->[0], $res->[1]);
Expand Down
2 changes: 1 addition & 1 deletion lib/Plack/Impl/Standalone.pm
Expand Up @@ -96,7 +96,7 @@ sub handle_connection {

open my $input, "<", \$buf;
$env->{'psgi.input'} = $input;
$res = $app->($env);
$res = Plack::Util::wrap_error { $app->($env) } $env;
last;
}
if ($reqlen == -2) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Plack/Test.pm
Expand Up @@ -372,7 +372,7 @@ my @TEST = (
},
sub {
my $env = shift;
die "Oops";
die "Throwing an exception from app handler. Server shouldn't crash.";
},
sub {
my $res = shift;
Expand Down
34 changes: 34 additions & 0 deletions lib/Plack/Util.pm
Expand Up @@ -47,6 +47,19 @@ sub foreach {
}
}

sub wrap_error(&$) {
my($code, $env) = @_;

local $@; my $res = eval { $code->() };
if ($@) {
my $body = "Internal Server Error";
$env->{'psgi.errors'}->print($@);
return [ 500, [ 'Content-Type' => 'text/plain', 'Content-Length' => length($body) ], [ $body ] ];
}

return $res;
}

sub inline_object {
my %args = @_;
bless {%args}, 'Plack::Util::Prototype';
Expand Down Expand Up @@ -127,6 +140,27 @@ guaranteed to be a I<line>) to the callback function.
It internally sets the buffer length C<$/> to 4096 in case it reads
the binary file, unless otherwise set in the caller's code.
=item wrap_error
my $res = Plack::Util::wrap_error { $app->($env) } $env;
Runs the code by wrapping erros and if an error is found, logs it to
C<< $env->{'psgi.errors'} >> and returns the template 500 Error response.
=item inline_object
my $o = Plack::Util::inline_object(
write => sub { $h->push_write(@_) },
close => sub { $h->push_shutdown },
);
$o->write(@stuff);
$o->close;
Creates an instant object that can react to methods passed in the
constructor. Handy to create when you need to create an IO stream
object for input or errors, as well as respone writer object for
L<PSGI::Async> extension.
=back
=cut
Expand Down
2 changes: 1 addition & 1 deletion t/090_impl/cgi.t
Expand Up @@ -12,7 +12,7 @@ Plack::Test->runtests(sub {
my ($name, $reqgen, $handler, $test) = @_;
note $name;
my $c = HTTP::Request::AsCGI->new($reqgen->())->setup;
Plack::Impl::CGI->new->run($handler);
eval { Plack::Impl::CGI->new->run($handler) };
$test->($c->response);
});

Expand Down

0 comments on commit 4154a68

Please sign in to comment.