~ubuntu-branches/ubuntu/edgy/libapache2-mod-perl2/edgy-updates

« back to all changes in this revision

Viewing changes to docs/api/APR/Error.pod

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2004-08-19 06:23:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040819062348-jxl4koqbtvgm8v2t
Tags: 1.99.14-4
Remove the LFS CFLAGS, and build-dep against apache2-*-dev (>= 2.0.50-10)
as we're backing out of the apache2/apr ABI transition.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=head1 NAME
 
2
 
 
3
APR::Error - Perl API for APR/Apache/mod_perl exceptions
 
4
 
 
5
 
 
6
 
 
7
 
 
8
=head1 Synopsis
 
9
 
 
10
  eval { $obj->mp_method() };
 
11
  if ($@ && $ref $@ eq 'APR::Error' && $@ == $some_code) {
 
12
      # handle the exception
 
13
  }
 
14
  else {
 
15
      die $@; # rethrow it
 
16
  }
 
17
 
 
18
 
 
19
=head1 Description
 
20
 
 
21
C<APR::Error> handles APR/Apache/mod_perl exceptions for you, while
 
22
leaving you in control.
 
23
 
 
24
Apache and APR API return status code for almost all methods, so if
 
25
you didn't check the return code and handled any possible problems,
 
26
you may have silent failures which may cause all kind of obscure
 
27
problems. On the other hand checking the status code after each call
 
28
is just too much of a kludge and makes quick prototyping/development
 
29
almost impossible, not talking about the code readability. Having
 
30
methods return status codes, also complicates the API if you need to
 
31
return other values.
 
32
 
 
33
Therefore to keep things nice and make the API readable we decided to
 
34
not return status codes, but instead throw exceptions with
 
35
C<APR::Error> objects for each method that fails. If you don't catch
 
36
those exceptions, everything works transparently - perl will intercept
 
37
the exception object and C<die()> with a proper error message. So you
 
38
get all the errors logged without doing any work.
 
39
 
 
40
Now, in certain cases you don't want to just die, but instead the
 
41
error needs to be trapped and handled. For example if some IO
 
42
operation times out, may be it is OK to trap that and try again. If we
 
43
were to die with an error message, you would have had to match the
 
44
error message, which is ugly, inefficient and may not work at all if
 
45
locale error strings are involved. Therefore you need to be able to
 
46
get the original status code that Apache or APR has generated. And the
 
47
exception objects give you that if you want to. Moreover the objects
 
48
contain additional information, such as the function name (in case you
 
49
were eval'ing several commands in one block), file and line number
 
50
where that function was invoked from. More attributes could be added
 
51
in the future.
 
52
 
 
53
C<APR::Error> uses method overloading, such that in boolean and
 
54
numerical contexts, the object returns the status code; in the string
 
55
context the full error message is returned.
 
56
 
 
57
When intercepting exceptions you need to check whether C<$@> is an
 
58
object (reference). If your application uses other exception objects
 
59
you additionally need to check whether this is a an C<APR::Error>
 
60
object. Therefore most of the time this is enough:
 
61
 
 
62
  eval { $obj->mp_method() };
 
63
  if ($@ && $ref $@ && $@ == $some_code)
 
64
      warn "handled exception: $@";
 
65
  }
 
66
 
 
67
But with other, non-mod_perl, exception objects you need to do:
 
68
 
 
69
  eval { $obj->mp_method() };
 
70
  if ($@ && $ref $@ eq 'APR::Error' && $@ == $some_code)
 
71
      warn "handled exception: $@";
 
72
  }
 
73
 
 
74
In theory you could even do:
 
75
 
 
76
  eval { $obj->mp_method() };
 
77
  if ($@ && $@ == $some_code)
 
78
      warn "handled exception: $@";
 
79
  }
 
80
 
 
81
but it's possible that the method will die with a plain string and not
 
82
an object, in which case C<$@ == $some_code> won't quite
 
83
work. Remember that mod_perl throws exception objects only when Apache
 
84
and APR fail, and in a few other special cases of its own (like
 
85
C<L<exit|docs::2.0::api::ModPerl::Util/C_exit_>>).
 
86
 
 
87
  warn "handled exception: $@" if $@ && $ref $@;
 
88
 
 
89
For example you wrote a code that performs L<a socket
 
90
read|docs::2.0::api::APR::Socket/C_recv_>:
 
91
 
 
92
  my $buff = $sock->recv(1024);
 
93
  my $rlen = length $buff;
 
94
  warn "read $rlen bytes\n";
 
95
 
 
96
and in certain cases it times out. The code will die and log the
 
97
reason for the failure, which is fine, but later on you decide that
 
98
you want to give the read another chance before dying. In which case
 
99
you rewrite the code to handle the exception like so:
 
100
 
 
101
  use APR::Const -compile => qw(TIMEUP);
 
102
  my $buff = eval { $sock->recv(1024) };
 
103
  if ($@) {
 
104
     die $@ unless ref $@ && $@ == APR::TIMEUP;
 
105
     goto retry;
 
106
  }
 
107
  my $rlen = length $buff;
 
108
  warn "read $rlen bytes\n";
 
109
 
 
110
Notice that we handle non-object and non-C<APR::Error> exceptions as
 
111
well, by simply rethrowing them.
 
112
 
 
113
 
 
114
Finally, the class is called C<APR::Error> because it needs to be used
 
115
outside mod_perl as well, when called from
 
116
C<L<APR|docs::2.0::api::APR>> applications written in perl.
 
117
 
 
118
 
 
119
=head1 API
 
120
 
 
121
=head2 C<cluck>
 
122
 
 
123
C<cluck> is an equivalent of C<Carp::cluck> that works with
 
124
C<APR::Error> exception objects.
 
125
 
 
126
=head2 C<confess>
 
127
 
 
128
C<confess> is an equivalent of C<Carp::confess> that works with
 
129
C<APR::Error> exception objects.
 
130
 
 
131
 
 
132
=head1 See Also
 
133
 
 
134
L<mod_perl 2.0 documentation|docs::2.0::index>.
 
135
 
 
136
 
 
137
 
 
138
 
 
139
=head1 Copyright
 
140
 
 
141
mod_perl 2.0 and its core modules are copyrighted under
 
142
The Apache Software License, Version 1.1.
 
143
 
 
144
 
 
145
 
 
146
 
 
147
=head1 Authors
 
148
 
 
149
L<The mod_perl development team and numerous
 
150
contributors|about::contributors::people>.
 
151
 
 
152
=cut
 
153