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";
84 abort_chain_on_error_fix=>1,
85 always_catch_http_exceptions=>1,
90 MyApp->setup_log('fatal');
93 $INC{'MyApp/Controller/Root.pm'} = __FILE__; # sorry...
94 MyApp->setup_log('error');
96 Test::More::ok(MyApp->setup);
98 ok my $psgi = MyApp->psgi_app;
99 test_psgi $psgi, sub {
101 my $res = $cb->(GET "/root/from_psgi_app");
103 like $res->content, qr/MyApp::Exception=HASH/;
106 test_psgi $psgi, sub {
108 my $res = $cb->(GET "/root/from_catalyst");
110 like $res->content, qr/MyApp::Exception=HASH/;
113 test_psgi $psgi, sub {
115 my $res = $cb->(GET "/root/from_code_type");
117 like $res->content, qr/MyApp::AnotherException=HASH/;
120 test_psgi $psgi, sub {
122 my $res = $cb->(GET "/root/classic_error");
124 like $res->content, qr'Ex Parrot', 'Ex Parrot';
127 test_psgi $psgi, sub {
129 my $res = $cb->(GET "/root/just_die");
131 like $res->content, qr'not dead yet', 'not dead yet';
134 # We need to specify the number of expected tests because tests that live
135 # in the callbacks might never get run (thus all ran tests pass but not all
136 # required tests run).