Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

M::HTTPExceptions - Fixes #266 #288

Closed
wants to merge 7 commits into from

2 participants

@wreis

No description provided.

miyagawa and others added some commits
@miyagawa miyagawa Set Host: header and HTTP_HOST based on $request->uri. Fixes #177 d047b53
@miyagawa miyagawa check defined c65f52b
Wallace Reis Merge branch 'master' of github.com:wreis/Plack e94c493
Wallace Reis #266 - Add tests 894a3ef
Wallace Reis HTTPExceptions streaming interface. Fixes #266
When the app dies after writer is retrieved in the streaming mode,
it might be late for setting HTTP headers, but maybe not so for the
code and body parts hence my suggestion of implementation.
47b7ae7
@miyagawa

As the other packages are using base instead of parent.

Owner

well, which packages? In Plack, we standardize to "use parent" almost (there seems some leftovers in .t files though).
Again, not that it matters much, just taking this as an opportunity to express my preference to be consistent.

@miyagawa

use 4 space indent, no spaces around parens.

@miyagawa
Owner

I really don't understand this patch. $respond should not be called twice once $writer is returned, no?

@wreis

Issue relies on the fact that although we got a $writer back, something might thrown an exception while streaming, see: wreis@894a3ef#L1R34.

@wreis wreis closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 31, 2012
  1. @miyagawa @wreis
  2. @miyagawa @wreis

    check defined

    miyagawa authored wreis committed
Commits on May 12, 2012
  1. Merge branch 'master' of github.com:wreis/Plack

    Wallace Reis authored
Commits on May 13, 2012
  1. #266 - Add tests

    Wallace Reis authored
  2. HTTPExceptions streaming interface. Fixes #266

    Wallace Reis authored
    When the app dies after writer is retrieved in the streaming mode,
    it might be late for setting HTTP headers, but maybe not so for the
    code and body parts hence my suggestion of implementation.
Commits on May 17, 2012
  1. @wreis

    use parent and fix style

    wreis authored
  2. @wreis

    use parent and fix style

    wreis authored
This page is out of date. Refresh to see the latest.
View
9 lib/Plack/Middleware/HTTPExceptions.pm
@@ -31,13 +31,8 @@ sub call {
try {
$res->(sub { return $writer = $respond->(@_) });
} catch {
- if ($writer) {
- Carp::cluck $_;
- $writer->close;
- } else {
- my $res = $self->transform_error($_, $env);
- $respond->($res);
- }
+ Carp::cluck $_;
+ $respond->( $self->transform_error($_, $env) );
};
};
}
View
22 t/Plack-Middleware/httpexceptions.t
@@ -2,6 +2,7 @@ use strict;
use Plack::Test;
use HTTP::Request::Common;
use Test::More;
+BEGIN { $ENV{'PLACK_ENV'} = 'deployment' }
package HTTP::Error;
sub new { bless {}, shift }
@@ -11,19 +12,28 @@ sub throw {
}
package HTTP::Error::InternalServerError;
-use base qw(HTTP::Error);
+use parent qw(HTTP::Error);
sub code { 500 }
package HTTP::Error::Forbidden;
-use base qw(HTTP::Error);
+use parent qw(HTTP::Error);
sub code { 403 }
sub as_string { "blah blah blah" }
package HTTP::Error::Redirect;
-use base qw(HTTP::Error);
+use parent qw(HTTP::Error);
sub code { 302 }
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;
my $app = sub {
@@ -38,12 +48,16 @@ my $app = sub {
};
use Plack::Middleware::HTTPExceptions;
+$app = MyMiddleware->wrap($app);
$app = Plack::Middleware::HTTPExceptions->wrap($app);
test_psgi $app, sub {
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->content, 'Internal Server Error';
View
37 t/Plack-Middleware/httpexceptions_streaming.t
@@ -2,6 +2,7 @@ use strict;
use Plack::Test;
use HTTP::Request::Common;
use Test::More;
+BEGIN { $ENV{'PLACK_ENV'} = 'deployment' }
package HTTP::Error;
sub new { bless {}, shift }
@@ -11,14 +12,32 @@ sub throw {
}
package HTTP::Error::InternalServerError;
-use base qw(HTTP::Error);
+use parent qw(HTTP::Error);
sub code { 500 }
package HTTP::Error::Forbidden;
-use base qw(HTTP::Error);
+use parent qw(HTTP::Error);
sub code { 403 }
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;
my $app = sub {
@@ -33,17 +52,29 @@ my $app = sub {
$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 };
};
use Plack::Middleware::HTTPExceptions;
+$app = MyMiddleware->wrap($app);
$app = Plack::Middleware::HTTPExceptions->wrap($app);
test_psgi $app, sub {
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->content, 'Internal Server Error';
Something went wrong with that request. Please try again.