Skip to content

Commit

Permalink
let an http-exception object that ->can("as_psgi") recieve the psgi $…
Browse files Browse the repository at this point in the history
…env as its first argument
  • Loading branch information
jjn1056 committed Dec 26, 2013
1 parent 7dafe51 commit 73830eb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/Plack/Middleware/HTTPExceptions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sub transform_error {

my($code, $message);
if (blessed $e && $e->can('as_psgi')) {
return $e->as_psgi;
return $e->as_psgi($env);
}
if (blessed $e && $e->can('code')) {
$code = $e->code;
Expand Down Expand Up @@ -123,7 +123,8 @@ the status message of the error code, such as I<Service Unavailable> for
C<503>.
Finally, the exception object may implement C<as_psgi>, and the result
of this will be returned directly as the PSGI response.
of this will be returned directly as the PSGI response. When calling
the C<as_psgi> method, the PSGI C<$env> will be passed.
If the code is in the 3xx range and the exception implements the 'location'
method (HTTP::Exception::3xx does), the Location header will be set in the
Expand Down
41 changes: 41 additions & 0 deletions t/Plack-Middleware/httpexceptions-as_psgi.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use strict;
use warnings;
use Plack::Test;
use HTTP::Request::Common;
use Plack::Middleware::HTTPExceptions;
use Test::More;

{
package MyApp::Exception::Tuple;

sub new {
my ($class, @args) = @_;
return bless +{res => \@args}, $class;
}

sub as_psgi {
my ($self, $env) = @_;
Test::More::ok $env && $env->{'psgi.version'},
'has $env and its a psgi $env';
return $self->{res};
}
}

ok my $psgi_app = sub {
my $env = shift;
die MyApp::Exception::Tuple->new(
404, ['content-type'=>'text/plain'], ['Not Found']);
};

ok $psgi_app = Plack::Middleware::HTTPExceptions->wrap($psgi_app);

test_psgi $psgi_app, sub {
my $cb = shift;
my $res = $cb->(GET "/");
is $res->code, 404;
is $res->content, 'Not Found';
};

# need to list the expected test number because of the test case in the
# exception class.
done_testing(5);

0 comments on commit 73830eb

Please sign in to comment.