1docs::api::APR::Error(3U)ser Contributed Perl Documentatidooncs::api::APR::Error(3)
2
3
4
6 APR::Error - Perl API for APR/Apache/mod_perl exceptions
7
9 eval { $obj->mp_method() };
10 if ($@ && $ref $@ eq 'APR::Error' && $@ == $some_code) {
11 # handle the exception
12 }
13 else {
14 die $@; # rethrow it
15 }
16
18 "APR::Error" handles APR/Apache/mod_perl exceptions for you, while
19 leaving you in control.
20
21 Apache and APR API return a status code for almost all methods, so if
22 you didn't check the return code and handled any possible problems, you
23 may have silent failures which may cause all kind of obscure problems.
24 On the other hand checking the status code after each call is just too
25 much of a kludge and makes quick prototyping/development almost
26 impossible, not talking about the code readability. Having methods
27 return status codes, also complicates the API if you need to return
28 other values.
29
30 Therefore to keep things nice and make the API readable we decided to
31 not return status codes, but instead throw exceptions with "APR::Error"
32 objects for each method that fails. If you don't catch those
33 exceptions, everything works transparently - perl will intercept the
34 exception object and die() with a proper error message. So you get all
35 the errors logged without doing any work.
36
37 Now, in certain cases you don't want to just die, but instead the error
38 needs to be trapped and handled. For example if some IO operation times
39 out, may be it is OK to trap that and try again. If we were to die with
40 an error message, you would have had to match the error message, which
41 is ugly, inefficient and may not work at all if locale error strings
42 are involved. Therefore you need to be able to get the original status
43 code that Apache or APR has generated. And the exception objects give
44 you that if you want to. Moreover the objects contain additional
45 information, such as the function name (in case you were eval'ing
46 several commands in one block), file and line number where that
47 function was invoked from. More attributes could be added in the
48 future.
49
50 "APR::Error" uses Perl operator overloading, such that in boolean and
51 numerical contexts, the object returns the status code; in the string
52 context the full error message is returned.
53
54 When intercepting exceptions you need to check whether $@ is an object
55 (reference). If your application uses other exception objects you
56 additionally need to check whether this is a an "APR::Error" object.
57 Therefore most of the time this is enough:
58
59 eval { $obj->mp_method() };
60 if ($@ && $ref $@ && $@ == $some_code)
61 warn "handled exception: $@";
62 }
63
64 But with other, non-mod_perl, exception objects you need to do:
65
66 eval { $obj->mp_method() };
67 if ($@ && $ref $@ eq 'APR::Error' && $@ == $some_code)
68 warn "handled exception: $@";
69 }
70
71 In theory you could even do:
72
73 eval { $obj->mp_method() };
74 if ($@ && $@ == $some_code)
75 warn "handled exception: $@";
76 }
77
78 but it's possible that the method will die with a plain string and not
79 an object, in which case "$@ == $some_code" won't quite work. Remember
80 that mod_perl throws exception objects only when Apache and APR fail,
81 and in a few other special cases of its own (like "exit").
82
83 warn "handled exception: $@" if $@ && $ref $@;
84
85 There are two ways to figure out whether an error fits your case. In
86 most cases you just compare $@ with an the error constant. For example
87 if a socket has a timeout set and the data wasn't read within the
88 timeout limit a "APR::Const::TIMEUP")
89
90 use APR::Const -compile => qw(TIMEUP);
91 $sock->timeout_set(1_000_000); # 1 sec
92 my $buff;
93 eval { $sock->recv($buff, BUFF_LEN) };
94 if ($@ && ref $@ && $@ == APR::Const::TIMEUP) {
95
96 }
97
98 However there are situations, where on different Operating Systems a
99 different error code will be returned. In which case to simplify the
100 code you should use the special subroutines provided by the
101 "APR::Status" class. One such condition is socket recv() timeout, which
102 on Unix throws the "EAGAIN" error, but on other system it throws a
103 different error. In this case "APR::Status::is_EAGAIN" should be used.
104
105 Let's look at a complete example. Here is a code that performs a socket
106 read:
107
108 my $rlen = $sock->recv(my $buff, 1024);
109 warn "read $rlen bytes\n";
110
111 and in certain cases it times out. The code will die and log the reason
112 for the failure, which is fine, but later on you may decide that you
113 want to have another attempt to read before dying and add some fine
114 grained sleep time between attempts, which can be achieved with
115 "select". Which gives us:
116
117 use APR::Status ();
118 # ....
119 my $tries = 0;
120 my $buffer;
121 RETRY: my $rlen = eval { $sock->recv($buffer, SIZE) };
122 if ($@)
123 die $@ unless ref $@ && APR::Status::is_EAGAIN($@);
124 if ($tries++ < 3) {
125 # sleep 250msec
126 select undef, undef, undef, 0.25;
127 goto RETRY;
128 }
129 else {
130 # do something else
131 }
132 }
133 warn "read $rlen bytes\n"
134
135 Notice that we handle non-object and non-"APR::Error" exceptions as
136 well, by simply re-throwing them.
137
138 Finally, the class is called "APR::Error" because it needs to be used
139 outside mod_perl as well, when called from "APR" applications written
140 in Perl.
141
143 "cluck"
144 "cluck" is an equivalent of "Carp::cluck" that works with "APR::Error"
145 exception objects.
146
147 "confess"
148 "confess" is an equivalent of "Carp::confess" that works with
149 "APR::Error" exception objects.
150
151 "strerror"
152 Convert APR error code to its string representation.
153
154 $error_str = APR::Error::strerror($rc);
155
156 ret: $rc ( "APR::Const status constant" )
157 The numerical value for the return (error) code
158
159 ret: $error_str ( string )
160 The string error message corresponding to the numerical value
161 inside $rc. (Similar to the C function strerror(3))
162
163 since: 2.0.00
164
165 Example:
166
167 Try to retrieve the bucket brigade, and if the return value doesn't
168 indicate success or end of file (usually in protocol handlers) die, but
169 give the user the human-readable version of the error and not just the
170 code.
171
172 my $rc = $c->input_filters->get_brigade($bb_in,
173 Apache2::Const::MODE_GETLINE);
174 if ($rc != APR::Const::SUCCESS && $rc != APR::Const::EOF) {
175 my $error = APR::Error::strerror($rc);
176 die "get_brigade error: $rc: $error\n";
177 }
178
179 It's probably a good idea not to omit the numerical value in the error
180 message, in case the error string is generated with non-English locale.
181
183 mod_perl 2.0 documentation.
184
186 mod_perl 2.0 and its core modules are copyrighted under The Apache
187 Software License, Version 2.0.
188
190 The mod_perl development team and numerous contributors.
191
192
193
194perl v5.36.0 2023-01-19 docs::api::APR::Error(3)