📄 error.pm
字号:
# # /*# * *********** WARNING **************# * This file generated by ModPerl::WrapXS/0.01# * Any changes made here will be lost# * ***********************************# * 01: lib/ModPerl/Code.pm:708# * 02: lib/ModPerl/WrapXS.pm:624# * 03: lib/ModPerl/WrapXS.pm:1173# * 04: Makefile.PL:423# * 05: Makefile.PL:325# * 06: Makefile.PL:56# */# package APR::Error;use strict;use warnings FATAL => 'all';use APR ();use APR::XSLoader ();our $VERSION = '0.009000';APR::XSLoader::load __PACKAGE__;require Carp;require Carp::Heavy;use APR::Util ();use overload nomethod => \&fatal, 'bool' => \&str, '==' => \&num_cmp, '!=' => \&num_cmp_not, '0+' => \&num, '""' => \&str;sub fatal { die __PACKAGE__ . ": Can't handle '$_[3]'" }# normally the object is created on the C side, but if you want to# create one from Perl, you can. just pass a hash with args:# rc, file, line, funcsub new { my $class = shift; my %args = @_; bless \%args, $class;}## - even though most of the time the error id is not useful to the end# users, developers may need to know it. For example in case of a# non-english user locale setting, the error string could be# incomprehensible to a developer, but by having the error id it's# possible to find the english equivalent# - the filename and line number are needed because perl doesn't# provide that info when exception objects are involvedsub str { return sprintf "%s: (%d) %s at %s line %d", $_[0]->{func}, $_[0]->{rc}, APR::Error::strerror($_[0]->{rc}), $_[0]->{file}, $_[0]->{line};}sub num { $_[0]->{rc} }sub num_cmp { $_[0]->{rc} == $_[1] }sub num_cmp_not { $_[0]->{rc} != $_[1] }# skip the wrappers from this package from the long callers trace$Carp::CarpInternal{+__PACKAGE__}++;# XXX: Carp::(confess|cluck) see no calls stack when Perl_croak is# called with Nullch (which is the way exception objects are# returned), so we fixup it here (doesn't quite work for croak# caller).sub cluck { if (ref $_[0] eq __PACKAGE__) { Carp::cluck("$_[0]->{func}: ($_[0]->{rc}) " . APR::Error::strerror($_[0]->{rc})); } else { &Carp::cluck; }}sub confess { if (ref $_[0] eq __PACKAGE__) { Carp::confess("$_[0]->{func}: ($_[0]->{rc}) " . APR::Error::strerror($_[0]->{rc})); } else { &Carp::confess; }}1;__END__=head1 NAMEAPR::Error - Perl API for APR/Apache/mod_perl exceptions=head1 Synopsis eval { $obj->mp_method() }; if ($@ && $ref $@ eq 'APR::Error' && $@ == $some_code) { # handle the exception } else { die $@; # rethrow it }=head1 DescriptionC<APR::Error> handles APR/Apache/mod_perl exceptions for you, whileleaving you in control.Apache and APR API return a status code for almost all methods, so ifyou didn't check the return code and handled any possible problems,you may have silent failures which may cause all kind of obscureproblems. On the other hand checking the status code after each callis just too much of a kludge and makes quick prototyping/developmentalmost impossible, not talking about the code readability. Havingmethods return status codes, also complicates the API if you need toreturn other values.Therefore to keep things nice and make the API readable we decided tonot return status codes, but instead throw exceptions withC<APR::Error> objects for each method that fails. If you don't catchthose exceptions, everything works transparently - perl will interceptthe exception object and C<die()> with a proper error message. So youget all the errors logged without doing any work.Now, in certain cases you don't want to just die, but instead theerror needs to be trapped and handled. For example if some IOoperation times out, may be it is OK to trap that and try again. If wewere to die with an error message, you would have had to match theerror message, which is ugly, inefficient and may not work at all iflocale error strings are involved. Therefore you need to be able toget the original status code that Apache or APR has generated. And theexception objects give you that if you want to. Moreover the objectscontain additional information, such as the function name (in case youwere eval'ing several commands in one block), file and line numberwhere that function was invoked from. More attributes could be addedin the future.C<APR::Error> uses Perl operator overloading, such that in boolean andnumerical contexts, the object returns the status code; in the stringcontext the full error message is returned.When intercepting exceptions you need to check whether C<$@> is anobject (reference). If your application uses other exception objectsyou additionally need to check whether this is a an C<APR::Error>object. Therefore most of the time this is enough: eval { $obj->mp_method() }; if ($@ && $ref $@ && $@ == $some_code) warn "handled exception: $@"; }But with other, non-mod_perl, exception objects you need to do: eval { $obj->mp_method() }; if ($@ && $ref $@ eq 'APR::Error' && $@ == $some_code) warn "handled exception: $@"; }In theory you could even do: eval { $obj->mp_method() }; if ($@ && $@ == $some_code) warn "handled exception: $@"; }but it's possible that the method will die with a plain string and notan object, in which case C<$@ == $some_code> won't quitework. Remember that mod_perl throws exception objects only when Apacheand APR fail, and in a few other special cases of its own (likeC<L<exit|docs::2.0::api::ModPerl::Util/C_exit_>>). warn "handled exception: $@" if $@ && $ref $@;There are two ways to figure out whether an error fits your case. Inmost cases you just compare C<$@> with an the error constant. Forexample if a socket has a timeout set and the data wasn't read withinthe timeout limit aC<L<APR::Const::TIMEUP|docs::2.0::api::APR::Const/C_APR__Const__TIMEUP_>>) use APR::Const -compile => qw(TIMEUP); $sock->timeout_set(1_000_000); # 1 sec my $buff; eval { $sock->recv($buff, BUFF_LEN) }; if ($@ && ref $@ && $@ == APR::Const::TIMEUP) { }However there are situations, where on different Operating Systems adifferent error code will be returned. In which case to simplify thecode you should use the special subroutines provided by theC<L<APR::Status|docs::2.0::api::APR::Status>> class. One suchcondition is socket C<recv()> timeout, which on Unix throws theC<EAGAIN> error, but on other system it throws a different error. Inthis caseC<L<APR::Status::is_EAGAIN|docs::2.0::api::APR::Status/C_is_EAGAIN_>>should be used.Let's look at a complete example. Here is a code that performs L<asocket read|docs::2.0::api::APR::Socket/C_recv_>: my $rlen = $sock->recv(my $buff, 1024); warn "read $rlen bytes\n";and in certain cases it times out. The code will die and log thereason for the failure, which is fine, but later on you may decidethat you want to have another attempt to read before dying and addsome fine grained sleep time between attempts, which can be achievedwith C<select>. Which gives us: use APR::Status (); # .... my $tries = 0; my $buffer; RETRY: my $rlen = eval { $sock->recv($buffer, SIZE) }; if ($@) die $@ unless ref $@ && APR::Status::is_EAGAIN($@); if ($tries++ < 3) { # sleep 250msec select undef, undef, undef, 0.25; goto RETRY; } else { # do something else } } warn "read $rlen bytes\n"Notice that we handle non-object and non-C<APR::Error> exceptions aswell, by simply re-throwing them.Finally, the class is called C<APR::Error> because it needs to be usedoutside mod_perl as well, when called fromC<L<APR|docs::2.0::api::APR>> applications written in Perl.=head1 API=head2 C<cluck>C<cluck> is an equivalent of C<Carp::cluck> that works withC<APR::Error> exception objects.=head2 C<confess>C<confess> is an equivalent of C<Carp::confess> that works withC<APR::Error> exception objects.=head2 C<strerror>Convert APR error code to its string representation. $error_str = APR::Error::strerror($rc);=over 4=item ret: C<$rc> ( C<L<APR::Const statusconstant|docs::2.0::api::APR::Const>> )The numerical value for the return (error) code=item ret: C<$error_str> ( string )The string error message corresponding to the numerical value insideC<$rc>. (Similar to the C function C<strerror(3)>)=item since: 2.0.00=backExample:Try to retrieve the bucket brigade, and if the return value doesn'tindicate success or end of file (usually in protocol handlers) die,but give the user the human-readable version of the error and not justthe code. my $rc = $c->input_filters->get_brigade($bb_in, Apache2::Const::MODE_GETLINE); if ($rc != APR::Const::SUCCESS && $rc != APR::Const::EOF) { my $error = APR::Error::strerror($rc); die "get_brigade error: $rc: $error\n"; }It's probably a good idea not to omit the numerical value in the errormessage, in case the error string is generated with non-Englishlocale.=head1 See AlsoL<mod_perl 2.0 documentation|docs::2.0::index>.=head1 Copyrightmod_perl 2.0 and its core modules are copyrighted underThe Apache Software License, Version 2.0.=head1 AuthorsL<The mod_perl development team and numerouscontributors|about::contributors::people>.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -