.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Plack::Middleware::HTTPExceptions 3pm" .TH Plack::Middleware::HTTPExceptions 3pm 2024-01-20 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Plack::Middleware::HTTPExceptions \- Catch HTTP exceptions .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use HTTP::Exception; \& \& my $app = sub { \& # ... \& HTTP::Exception::500\->throw; \& }; \& \& builder { \& enable "HTTPExceptions", rethrow => 1; \& $app; \& }; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Plack::Middleware::HTTPExceptions is a PSGI middleware component to catch exceptions from applications that can be translated into HTTP status codes. .PP Your application is supposed to throw an object that implements a \&\f(CW\*(C`code\*(C'\fR method which returns the HTTP status code, such as 501 or 404. This middleware catches them and creates a valid response out of the code. If the \f(CW\*(C`code\*(C'\fR method returns a code that is not an HTTP redirect or error code (3xx, 4xx, or 5xx), the exception will be rethrown. .PP The exception object may also implement \f(CW\*(C`as_string\*(C'\fR or overload stringification to represent the text of the error. The text defaults to the status message of the error code, such as \fIService Unavailable\fR for \&\f(CW503\fR. .PP Finally, the exception object may implement \f(CW\*(C`as_psgi\*(C'\fR, and the result of this will be returned directly as the PSGI response. .PP 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 response, so you can do redirects this way. .PP There are CPAN modules HTTP::Exception and HTTP::Throwable, and they are perfect to throw from your application to let this middleware catch and display, but you can also implement your own exception class to throw. .PP If the thrown exception is not an object that implements either a \&\f(CW\*(C`code\*(C'\fR or an \f(CW\*(C`as_psgi\*(C'\fR method, a 500 error will be returned, and the exception is printed to the psgi.errors stream. Alternatively, you can pass a true value for the \f(CW\*(C`rethrow\*(C'\fR parameter for this middleware, and the exception will instead be rethrown. This is enabled by default when \f(CW\*(C`PLACK_ENV\*(C'\fR is set to \f(CW\*(C`development\*(C'\fR, so that the StackTrace middleware can catch it instead. .SH AUTHOR .IX Header "AUTHOR" Tatsuhiko Miyagawa .SH "SEE ALSO" .IX Header "SEE ALSO" paste.httpexceptions HTTP::Exception HTTP::Throwable