summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2018-02-22 14:17:11 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-02-22 14:17:11 (GMT)
commita69997afd7c96a6f6c91a6a88653c37387b09190 (patch) (side-by-side diff)
tree11802f0584b088729127814ff561993109f0b86b
parentc16f2363476167d59a2eaad38ef9e98f032ebdd1 (diff)
downloadapache-defaults-a69997afd7c96a6f6c91a6a88653c37387b09190.tar.gz
apache-defaults-a69997afd7c96a6f6c91a6a88653c37387b09190.tar.bz2
Provide alternative error handling
New constructor parameter on_error controls error handling.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--lib/Apache/Defaults.pm103
1 files changed, 72 insertions, 31 deletions
diff --git a/lib/Apache/Defaults.pm b/lib/Apache/Defaults.pm
index 08c4865..b980333 100644
--- a/lib/Apache/Defaults.pm
+++ b/lib/Apache/Defaults.pm
@@ -15,5 +15,12 @@ sub new {
my $class = shift;
- my $self = bless {}, $class;
+ my $self = bless { on_error => 'croak' }, $class;
local %_ = @_;
my $v;
+
+ if (my $v = delete $_{on_error}) {
+ croak "invalid on_error value"
+ unless grep { $_ eq $v } qw(croak return);
+ $self->{on_error} = $v;
+ }
+
my @servlist;
@@ -32,2 +39,5 @@ sub new {
$self->{server} = shift @select;
+ } elsif ($self->{on_error} eq 'return') {
+ $self->{status} = 127;
+ $self->{error} = "No suitable httpd binary found";
} else {
@@ -40,3 +50,9 @@ sub new {
if ($env->status) {
- croak "Got status ".$env->status." trying to inherit environment";
+ if ($self->{on_error} eq 'return') {
+ $self->{status} = $env->status;
+ $self->{error} = "Failed to inherit environment";
+ } else {
+ croak sprintf("Got status %d trying to inherit environment",
+ $env->status);
+ }
}
@@ -46,2 +62,6 @@ sub new {
croak "unrecognized arguments" if keys(%_);
+
+ $self->_get_version_info unless $self->status;
+ $self->_get_module_info unless $self->status;
+
return $self;
@@ -69,12 +89,18 @@ sub probe {
waitpid($pid, 0);
- if ($? == -1) {
- croak "failed to execute " .$self->server . ": $!";
- } elsif ($? & 127) {
- croak sprintf("%s died with signal %d%s",
- $self->server, $? & 127,
- ($? & 128) ? ' (core dumped)' : '');
- } elsif (my $code = $? >> 8) {
- local $/ = undef;
- croak sprintf("%s terminated with status %d; error message: %s",
- $self->server, $code, <$err>);
+ if ($self->{on_error} eq 'croak') {
+ if ($? == -1) {
+ croak "failed to execute " .$self->server . ": $!";
+ } elsif ($? & 127) {
+ croak sprintf("%s died with signal %d%s",
+ $self->server, $? & 127,
+ ($? & 128) ? ' (core dumped)' : '');
+ } elsif (my $code = $? >> 8) {
+ local $/ = undef;
+ croak sprintf("%s terminated with status %d; error message: %s",
+ $self->server, $code, <$err>);
+ }
+ } elsif ($?) {
+ local $/ = undef;
+ $self->{status} = $?;
+ $self->{error} = <$err>;
}
@@ -96,4 +122,3 @@ sub _get_version_info {
my $self = shift;
- unless ($self->{has_version_info}) {
- $self->probe(sub {
+ $self->probe(sub {
local $_ = shift;
@@ -131,7 +156,6 @@ sub _get_version_info {
}, '-V');
- }
- $self->{has_version_info} = 1;
}
-my @ATTRIBUTES = qw(name
+my @ATTRIBUTES = qw(status error
+ name
version
@@ -148,7 +172,3 @@ my @ATTRIBUTES = qw(name
foreach my $attribute (@ATTRIBUTES) {
- *{ __PACKAGE__ . '::' . $attribute } = sub {
- my $self = shift;
- $self->_get_version_info;
- $self->{$attribute};
- }
+ *{ __PACKAGE__ . '::' . $attribute } = sub { shift->{$attribute} }
}
@@ -160,3 +180,2 @@ sub defines {
my $self = shift;
- $self->_get_version_info;
if (@_) {
@@ -292,3 +311,2 @@ sub preloaded {
my $self = shift;
- $self->_get_module_info;
if (@_) {
@@ -301,4 +319,3 @@ sub _get_module_info {
my $self = shift;
- unless ($self->{has_module_info}) {
- $self->probe(sub {
+ $self->probe(sub {
local $_ = shift;
@@ -310,4 +327,2 @@ sub _get_module_info {
}, '-l');
- $self->{has_module_info} = 1;
- }
}
@@ -373,3 +388,11 @@ such problems, e.g.:
$x = new Apache::Defaults(environ => /etc/apache2/envvars)
-
+
+=item C<on_error>
+
+Controls the error handling. Allowed values are C<croak> and C<return>.
+If the value is C<croak> (the default), the method will I<croak> if an
+error occurs. If set to C<return>, the constructor will return a valid
+object. The B<httpd> exit status and diagnostics emitted to the stderr
+will be available via the B<status> and B<error> methods.
+
=back
@@ -379,2 +402,20 @@ is not found or exits with failure).
+=head2 status
+
+ $x = new Apache::Defaults(on_error => 'return');
+ if ($x->status) {
+ die $x->error;
+ }
+
+Returns the status of the last B<httpd> invocation (i.e. the value of
+the B<$?> perl variable after B<waitpid>). The caller should inspect
+this value, after constructing an B<Apache::Defaults> object with
+the C<on_error> attribute set to C<return>.
+
+=head2 error
+
+Returns additional diagnostics if B<$x-E<gt>status != 0>. Normally, these are
+diagnostic messages that B<httpd> printed to standard error before
+termination.
+
=head2 server
@@ -389,3 +430,3 @@ Returns the pathname of the B<httpd> binary.
-Returns full command line of the B<httpd> binary.
+Returns the full command line of the B<httpd> binary.
@@ -393,3 +434,3 @@ Returns full command line of the B<httpd> binary.
- $hashref = $x->environ
+ $hashref = $x->environ;

Return to:

Send suggestions and report system problems to the System administrator.