HTTP::Exception(3pm)
π HTTP::Exception π
okay so bestie, we're in the HTTP::Exception(3pm) page from the User Contributed Perl Documentation, and honestly? it's a bop. it's giving "throw your HTTP errors as real, catchable exceptions" and i am obsessed. π
β¨ NAME β¨
HTTP::Exception β throw HTTP-Errors as (Exception::Class-) Exceptions π«Ά
πΈ VERSION πΈ
version 0.04007 β she's vintage, she's stable, she's that girl π
π¦ SYNOPSIS π¦
HTTP::Exception lets you throw HTTP-Errors as Exceptions. periodt.
use HTTP::Exception;
# throw a 404 Exception
HTTP::Exception->throw(404);
# later in your framework
eval { ... };
if (my $e = HTTP::Exception->caught) {
# do some errorhandling stuff
print $e->code; # 404
print $e->status_message; # Not Found
}
You can also throw HTTP::Exception-subclasses like this, no cap.
# same 404 Exception
eval { HTTP::Exception::404->throw(); };
eval { HTTP::Exception::NOT_FOUND->throw(); };
And catch them accordingly, because catching is the whole vibe.
# same 404 Exception
eval { HTTP::Exception::404->throw(); };
if (my $e = HTTP::Exception::405->caught) { do stuff } # won't catch
if (my $e = HTTP::Exception::404->caught) { do stuff } # will catch
if (my $e = HTTP::Exception::NOT_FOUND->caught) { do stuff } # will catch
if (my $e = HTTP::Exception::4XX->caught) { do stuff } # will catch all 4XX Exceptions
if (my $e = HTTP::Exception->caught) { do stuff } # will catch every HTTP::Exception
if (my $e = Exception::Class->caught) { do stuff } # catch'em all
You can create Exceptions and not throw them, because maybe you want to set some fields manually first. See "FIELDS" in HTTP::Exception and "ACCESSORS" in HTTP::Exception for more info, bestie.
# is not thrown, ie doesn't die, only created
my $e = HTTP::Exception->new(404);
# usual stuff works
$e->code; # 404
$e->status_message # Not Found
# set status_message to something else
$e->status_message('Nothing Here')
# fails, because code is only an accessor, see section ACCESSORS below
# $e->code(403);
# and finally throw our prepared exception
$e->throw;
π DESCRIPTION π
Every HTTP::Exception is a Exception::Class - Class. So the same mechanisms apply as with Exception::Class-classes. fr fr, go look at Exception::Class's docs for the more general tea on exceptions, and Exception::Class::Base for info on what methods a caught exception also has. π
HTTP::Exception is only a factory for HTTP::Exception::XXX (where X is a number) subclasses. That means that HTTP::Exception->new(404) returns a HTTP::Exception::404 object, which in turn is a HTTP::Exception::Base - Object. it's giving layers, and we love that. π
Don't bother checking a caught HTTP::Exception::...-class with "isa" as it might not contain what you would expect. Use the code- or status_message-attributes and the is_ -methods instead. trust. π
The subclasses are created at compile-time, ie the first time you make "use HTTP::Exception". See paragraph below for the naming scheme of those subclasses.
Subclassing the subclasses works as expected. as it should. π
π NAMING SCHEME π
HTTP::Exception::XXX
X is a Number and XXX is a valid HTTP-Statuscode. All HTTP-Statuscodes are supported. See chapter "COMPLETENESS" in HTTP::Exception π
HTTP::Exception::STATUS_MESSAGE
STATUS_MESSAGE is the same name as a HTTP::Status Constant WITHOUT the HTTP_ at the beginning. So see "CONSTANTS" in HTTP::Status for more details. π
β IMPORTING SPECIFIC ERROR RANGES β
It is possible to load only specific ranges of errors. For example:
use HTTP::Exception qw(5XX);
HTTP::Exception::500->throw; # works
HTTP::Exception::400->throw; # won't work anymore
will only create HTTP::Exception::500 till HTTP::Exception::510. In theory this should save some memory, but I don't have any numbers, that back up this claim. (ngl, honest of them π)
You can load multiple ranges:
use HTTP::Exception qw(3XX 4XX 5XX);
And there are aliases for ranges:
use HTTP::Exception qw(CLIENT_ERROR)
The following aliases exist and load the specified ranges:
REDIRECTION => 3XX
CLIENT_ERROR => 4XX
SERVER_ERROR => 5XX
ERROR => 4XX 5XX
ALL => 1XX 2XX 3XX 4XX 5XX
And of course, you can load multiple aliased ranges:
use HTTP::Exception qw(REDIRECTION ERROR)
ALL is the same as not specifying any specific range. literally the same thing.
# the same
use HTTP::Exception qw(ALL);
use HTTP::Exception;
π ACCESSORS (READONLY) π
code
A valid HTTP-Statuscode. See HTTP::Status for information on what codes exist. π₯
is_info
Return TRUE if "$self-"code> is an Informational status code (1xx). This class of status code indicates a provisional response which can't have any content.
is_success
Return TRUE if "$self-"code> is a Successful status code (2xx). β¨
is_redirect
Return TRUE if "$self-"code> is a Redirection status code (3xx). This class if status code indicates that further action needs to be taken by the user agent in order to fulfill the request.
is_error
Return TRUE if "$self-"code> is an Error status code (4xx or 5xx). The function return TRUE for both client error or a server error status codes. equal opportunity error detection π
is_client_error
Return TRUE if "$self-"code> is an Client Error status code (4xx). This class of status code is intended for cases in which the client seems to have erred. (it's giving "not my fault, it's the client's" π)
is_server_error
Return TRUE if "$self-"code> is an Server Error status code (5xx). This class of status codes is intended for cases in which the server is aware that it has erred or is incapable of performing the request.
POD foris_methods is Copy/Pasted fromHTTP::Status, so check back there and alert me of changes. π«Ά
π§ FIELDS π§
Fields are the same as ACCESSORS except they can be set. Either you set them during Exception creation (->new) or Exception throwing (->throw).
HTTP::Exception->new(200, status_message => "Everything's fine");
HTTP::Exception::200->new(status_message => "Everything's fine");
HTTP::Exception::OK->new(status_message => "Everything's fine");
HTTP::Exception->throw(200, status_message => "Everything's fine");
HTTP::Exception::200->throw(status_message => "Everything's fine");
HTTP::Exception::OK->throw(status_message => "Everything's fine");
Catch them in your Webframework like this:
eval { ... }
if (my $e = HTTP::Exception->caught) {
print $e->code; # 200
print $e->status_message # "Everything's fine" instead of the usual ok
}
status_message
DEFAULT The HTTP-Statusmessage as provided by HTTP::Status
A Message, that represents the Execptions' Status for Humans. π
π PLACK π
HTTP::Exception can be used with Plack::Middleware::HTTPExceptions. But HTTP::Exception does not depend on Plack, you can use it anywhere else. It just plays nicely with Plack. she's not clingy, she's just compatible π
π COMPLETENESS π
For the sake of completeness, HTTP::Exception provides exceptions for non-error-http-statuscodes. This means you can do
HTTP::Exception->throw(200);
which throws an Exception of type OK. Maybe useless, but complete. A more realworld-example would be a redirection:
# all are exactly the same
HTTP::Exception->throw(301, location => 'google.com');
HTTP::Exception::301->throw(location => 'google.com');
HTTP::Exception::MOVED_PERMANENTLY->throw(location => 'google.com');
π CAVEATS π
The HTTP::Exception-Subclass-Creation relies on HTTP::Status. It's possible that the Subclasses change, when HTTP::Status' constants are changed. π
New Subclasses are created automatically, when constants are added to HTTP::Status. That means in turn, that Subclasses disappear, when constants are removed from HTTP::Status. it's giving fragile, but okay.
Some constants were added toHTTP::Status' in February 2012. As a resultHTTP::Exceptionbroke. But that was the result of uncareful coding on my side. I think, that breaking changes are now quite unlikely. π
π«Ά AUTHOR π«Ά
Thomas Mueller, "<tmueller at cpan.org>" β the icon, the moment, the legend π
πΈ SEE ALSO πΈ
Exception::Class, Exception::Class::Base
Consult Exception::Class' documentation for the Exception-Mechanism and Exception::Class::Base' docs for a list of methods our caught Exception is also capable of. π¦
HTTP::Status
Constants, Statuscodes and Statusmessages
HTTP::Throwable, built on top of the more modern Throwable framework (the successor to Exception::Class)
Plack, especially Plack::Middleware::HTTPExceptions
Have a look at Plack, because it rules in general. In the first place, this Module was written as the companion for Plack::Middleware::HTTPExceptions, but since it doesn't depend on Plack, you can use it anywhere else, too. π₯
π BUGS π
Please report any bugs or feature requests to "bug-http-exception at rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTTP-Exception>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. bestie, that's a whole communication pipeline, slay π
π SUPPORT π
You can find documentation for this module with the perldoc command.
perldoc HTTP::Exception
You can also look for information at:
- RT: CPAN's request tracker
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTTP-Exception>
- AnnoCPAN: Annotated CPAN documentation
<http://annocpan.org/dist/HTTP-Exception>
- CPAN Ratings
<http://cpanratings.perl.org/d/HTTP-Exception>
- Search CPAN
<https://metacpan.org/release/HTTP-Exception>
β LICENSE AND COPYRIGHT β
Copyright 2010 Thomas Mueller.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See <http://dev.perl.org/licenses/> for more information. π
okay and that's the whole thing!! this page came to you via perl v5.26.2, dated 2018-07-28 β an absolute time capsule, and honestly? she ate. π
β¨ mwah, go throw some exceptions responsibly, bestie π«Άπ
π¦ where this page comes from
- debian bookworm: libhttp-exception-perl 0.04007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libhttp-exception-perl 0.04007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libhttp-exception-perl 0.04007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libhttp-exception-perl 0.04007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms