Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M::HTTPExceptions - Fixes #266 #288

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/Plack/Middleware/HTTPExceptions.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ sub call {
try { try {
$res->(sub { return $writer = $respond->(@_) }); $res->(sub { return $writer = $respond->(@_) });
} catch { } catch {
if ($writer) { Carp::cluck $_;
Carp::cluck $_; $respond->( $self->transform_error($_, $env) );
$writer->close;
} else {
my $res = $self->transform_error($_, $env);
$respond->($res);
}
}; };
}; };
} }
Expand Down
22 changes: 18 additions & 4 deletions t/Plack-Middleware/httpexceptions.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use strict;
use Plack::Test; use Plack::Test;
use HTTP::Request::Common; use HTTP::Request::Common;
use Test::More; use Test::More;
BEGIN { $ENV{'PLACK_ENV'} = 'deployment' }


package HTTP::Error; package HTTP::Error;
sub new { bless {}, shift } sub new { bless {}, shift }
Expand All @@ -11,19 +12,28 @@ sub throw {
} }


package HTTP::Error::InternalServerError; package HTTP::Error::InternalServerError;
use base qw(HTTP::Error); use parent qw(HTTP::Error);
sub code { 500 } sub code { 500 }


package HTTP::Error::Forbidden; package HTTP::Error::Forbidden;
use base qw(HTTP::Error); use parent qw(HTTP::Error);
sub code { 403 } sub code { 403 }
sub as_string { "blah blah blah" } sub as_string { "blah blah blah" }


package HTTP::Error::Redirect; package HTTP::Error::Redirect;
use base qw(HTTP::Error); use parent qw(HTTP::Error);
sub code { 302 } sub code { 302 }
sub location { "http://somewhere/else" } sub location { "http://somewhere/else" }


package MyMiddleware;
use parent 'Plack::Middleware';
sub call {
my ($self, $env) = @_;
die 'Unknown error, but on test purpose'
if $env->{'PATH_INFO'} eq '/unknow_error';
return $self->app->($env);
}

package main; package main;


my $app = sub { my $app = sub {
Expand All @@ -38,12 +48,16 @@ my $app = sub {
}; };


use Plack::Middleware::HTTPExceptions; use Plack::Middleware::HTTPExceptions;
$app = MyMiddleware->wrap($app);
$app = Plack::Middleware::HTTPExceptions->wrap($app); $app = Plack::Middleware::HTTPExceptions->wrap($app);


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


my $res = $cb->(GET "/"); my $res = $cb->(GET '/unknow_error');
is $res->code, 500;
is $res->content, 'Internal Server Error';
$res = $cb->(GET "/");
is $res->code, 500; is $res->code, 500;
is $res->content, 'Internal Server Error'; is $res->content, 'Internal Server Error';


Expand Down
37 changes: 34 additions & 3 deletions t/Plack-Middleware/httpexceptions_streaming.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use strict;
use Plack::Test; use Plack::Test;
use HTTP::Request::Common; use HTTP::Request::Common;
use Test::More; use Test::More;
BEGIN { $ENV{'PLACK_ENV'} = 'deployment' }


package HTTP::Error; package HTTP::Error;
sub new { bless {}, shift } sub new { bless {}, shift }
Expand All @@ -11,14 +12,32 @@ sub throw {
} }


package HTTP::Error::InternalServerError; package HTTP::Error::InternalServerError;
use base qw(HTTP::Error); use parent qw(HTTP::Error);
sub code { 500 } sub code { 500 }


package HTTP::Error::Forbidden; package HTTP::Error::Forbidden;
use base qw(HTTP::Error); use parent qw(HTTP::Error);
sub code { 403 } sub code { 403 }
sub as_string { "blah blah blah" } sub as_string { "blah blah blah" }


{
no strict 'refs';
*Carp::cluck = sub {};
}
package MyMiddleware;
use parent 'Plack::Middleware';
sub call {
my ($self, $env) = @_;
my $res = $self->app->($env);
$self->response_cb($res, sub {
return sub {
die 'Unknown error, but on test purpose'
if $env->{PATH_INFO} eq '/unknow_error';
return shift;
};
});
}

package main; package main;


my $app = sub { my $app = sub {
Expand All @@ -33,17 +52,29 @@ my $app = sub {
$w->close; $w->close;
}; };
} }
elsif ( $env->{PATH_INFO} eq '/unknow_error' ) {
return sub {
my $res = shift;
my $w = $res->([ 200, [ 'Content-Type', 'text/plain' ] ]);
$w->write("Hello");
$w->close;
};
}


return sub { HTTP::Error::InternalServerError->throw }; return sub { HTTP::Error::InternalServerError->throw };
}; };


use Plack::Middleware::HTTPExceptions; use Plack::Middleware::HTTPExceptions;
$app = MyMiddleware->wrap($app);
$app = Plack::Middleware::HTTPExceptions->wrap($app); $app = Plack::Middleware::HTTPExceptions->wrap($app);


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


my $res = $cb->(GET "/"); my $res = $cb->(GET '/unknow_error');
is $res->code, 500;
is $res->content, 'Internal Server Error';
$res = $cb->(GET "/");
is $res->code, 500; is $res->code, 500;
is $res->content, 'Internal Server Error'; is $res->content, 'Internal Server Error';


Expand Down