4 use HTTP::Request::Common;
5 use HTTP::Message::PSGI;
9 # Test to make sure HTTP style exceptions do NOT bubble up to the middleware
10 # if the backcompat setting 'always_catch_http_exceptions' is enabled.
13 package MyApp::Exception;
16 my ($class, $code, $headers, $body) = @_;
17 return bless +{res => [$code, $headers, $body]}, $class;
20 sub throw { die shift->new(@_) }
23 my ($self, $env) = @_;
24 my ($code, $headers, $body) = @{$self->{res}};
26 return [$code, $headers, $body]; # for now
29 my $responder = shift;
30 $responder->([$code, $headers, $body]);
34 package MyApp::AnotherException;
36 sub new { bless +{}, shift }
40 sub as_string { 'bad stringy bad' }
42 package MyApp::Controller::Root;
44 use base 'Catalyst::Controller';
48 die MyApp::Exception->new(
49 404, ['content-type'=>'text/plain'], ['Not Found']);
52 sub from_psgi_app :Local {
54 $c->res->from_psgi_response(
59 sub from_catalyst :Local {
61 MyApp::Exception->throw(
62 403, ['content-type'=>'text/plain'], ['Forbidden']);
65 sub from_code_type :Local {
66 my $e = MyApp::AnotherException->new;
70 sub classic_error :Local {
72 Catalyst::Exception->throw("Ex Parrot");
77 die "I'm not dead yet";
80 sub end : ActionClass('RenderView') {}
86 abort_chain_on_error_fix=>1,
87 always_catch_http_exceptions=>1,
92 MyApp->setup_log('fatal');
95 $INC{'MyApp/Controller/Root.pm'} = __FILE__; # sorry...
96 MyApp->setup_log('error');
98 Test::More::ok(MyApp->setup);
100 ok my $psgi = MyApp->psgi_app;
101 test_psgi $psgi, sub {
103 my $res = $cb->(GET "/root/from_psgi_app");
105 like $res->content, qr/MyApp::Exception=HASH/;
108 test_psgi $psgi, sub {
110 my $res = $cb->(GET "/root/from_catalyst");
112 like $res->content, qr/MyApp::Exception=HASH/;
115 test_psgi $psgi, sub {
117 my $res = $cb->(GET "/root/from_code_type");
119 like $res->content, qr/MyApp::AnotherException=HASH/;
122 test_psgi $psgi, sub {
124 my $res = $cb->(GET "/root/classic_error");
126 like $res->content, qr'Ex Parrot', 'Ex Parrot';
129 test_psgi $psgi, sub {
131 my $res = $cb->(GET "/root/just_die");
133 like $res->content, qr'not dead yet', 'not dead yet';
136 # We need to specify the number of expected tests because tests that live
137 # in the callbacks might never get run (thus all ran tests pass but not all
138 # required tests run).