Skip to content

Commit

Permalink
Merge 9331e2f into eac48d5
Browse files Browse the repository at this point in the history
  • Loading branch information
karenetheridge committed May 27, 2013
2 parents eac48d5 + 9331e2f commit 3199c97
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Go to http://github.com/plack/Plack/issues for the roadmap and known issues.

{{$NEXT}}
[IMPROVEMENTS]
- Plack::Middleware::HTTPExceptions can now optionally send uncaught
exceptions to psgix.logger, instead of psgi.errors. (ether)

1.0024 2013-05-01 10:05:56 PDT
[IMPROVEMENTS]
Expand Down
3 changes: 3 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ installer = MakeMaker

[Metadata]
x_authority = cpan:MIYAGAWA

[Prereqs / TestRecommends]
Test::Deep = 0
20 changes: 16 additions & 4 deletions lib/Plack/Middleware/HTTPExceptions.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Plack::Middleware::HTTPExceptions;
use strict;
use parent qw(Plack::Middleware);
use Plack::Util::Accessor qw(rethrow);
use Plack::Util::Accessor qw(rethrow use_logger);

use Carp ();
use Try::Tiny;
Expand Down Expand Up @@ -60,7 +60,18 @@ sub transform_error {
}
else {
$code = 500;
$env->{'psgi.errors'}->print($e);
if ($self->use_logger) {
if (defined $env->{'psgix.logger'}) {
$env->{'psgix.logger'}->({level=>'fatal',message=>$e});
}
else {
$env->{'psgi.errors'}->print('no psgix.logger set - falling back to psgi.errors!');
$env->{'psgi.errors'}->print($e);
}
}
else {
$env->{'psgi.errors'}->print($e);
}
}
}

Expand Down Expand Up @@ -135,8 +146,9 @@ catch and display, but you can also implement your own exception class
to throw.
If the thrown exception is not an object that implements either a
C<code> or an C<as_psgi> method, a 500 error will be returned, and the
exception is printed to the psgi.errors stream.
C<code> or an C<as_psgi> method, a C<500> error will be returned, and the
exception is sent as a fatal message to C<psgix.logger> if the C<use_logger>
option is set, or printed to the C<psgi.errors> stream otherwise.
Alternatively, you can pass a true value for the C<rethrow> parameter
for this middleware, and the exception will instead be rethrown. This is
enabled by default when C<PLACK_ENV> is set to C<development>, so that
Expand Down
36 changes: 36 additions & 0 deletions t/Plack-Middleware/httpexceptions.t
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,40 @@ test_psgi $app, sub {
like $psgi_errors, qr/ugly stack trace here/;
};

SKIP: {
eval { require Test::Deep; 1 }
or skip 'Test::Deep required for remaining tests', 1;

my $log_stash;

my $app2 = sub {
my $env = shift;
$env->{'psgi.errors'} = undef; # will die if we attempt to use
$env->{'psgix.logger'} = sub { push @$log_stash, shift };
die 'ugly stack trace here';
};
$app2 = Plack::Middleware::HTTPExceptions->wrap(
$app2,
use_logger => 1,
);

test_psgi $app2, sub {
my $cb = shift;

# falls through to returning HTTP 500
my $res = $cb->(GET "/uncaught");

Test::Deep::cmp_deeply(
$log_stash,
[
{
level => 'fatal',
message => Test::Deep::re(qr/^ugly stack trace here/)
}
],
'psgix.logger is used, rather than psgi.errors',
);
};
}

done_testing;

0 comments on commit 3199c97

Please sign in to comment.