[NUUG fiksgatami] [patch] Use throw to report errors in mySociety::Config

Petter Reinholdtsen pere at hungry.com
Sat Jan 24 11:00:00 CET 2009


When there is an error in the configuration, an empty Error exception
is displayed on the front page.  The reason is that the die messages
sent from the mySociety::Config module is not made part of any Error
exception, and thus not displayed.  I recommend changing the
mySociety::Config module to use throw instead of die to report
problems.  Here is a patch to do just that.  With this change in
place, it was trivial to figure out which configuration variable was
incorrect.

Index: perllib/mySociety/Config.pm
===================================================================
RCS file: /repos/mysociety/perllib/mySociety/Config.pm,v
retrieving revision 1.18
diff -u -3 -p -r1.18 Config.pm
--- perllib/mySociety/Config.pm 10 Apr 2008 15:41:40 -0000      1.18
+++ perllib/mySociety/Config.pm 24 Jan 2009 09:57:26 -0000
@@ -52,7 +52,7 @@ sub find_php () {
             return "$dir/$name" if (-x "$dir/$name");
         }
     }
-    die "unable to locate PHP binary, needed to read config file";
+    throw Error::Simple "unable to locate PHP binary, needed to read config file";
 }

 =item read_config FILE [DEFAULTS]
@@ -68,7 +68,7 @@ sub read_config ($;$) {
     my ($f, $defaults) = @_;

     if (! -r $f) {
-        die "$f: read permissions not OK for config file";
+        throw Error::Simple "$f: read permissions not OK for config file";
     }

     my $old_SIGCHLD = $SIG{CHLD};
@@ -89,7 +89,7 @@ sub read_config ($;$) {
     my $p2 = new IO::Pipe($inr, $inw);

     my $pid = fork();
-    die "fork: $!" unless (defined($pid));
+    throw Error::Simple "fork: $!" unless (defined($pid));
     if ($pid == 0) {
         # Delete everything from the environment other than our special
         # variable to give PHP the config file name. We don't want PHP to pick
@@ -107,7 +107,7 @@ sub read_config ($;$) {
         $inr->close();
         $outw->close();

-        exec($php_path) or die "$php_path: exec: $!";
+        exec($php_path) or throw Error::Simple "$php_path: exec: $!";
     }

     $inr->close();
@@ -138,9 +138,9 @@ EOF

     if (!defined($line)) {
         if ($outr->error()) {
-            die "$php_path: $f: $!";
+            throw Error::Simple "$php_path: $f: $!";
         } else {
-            die "$php_path: $f: no option output from subprocess";
+            throw Error::Simple "$php_path: $f: no option output from subprocess";
         }
     }

@@ -151,7 +151,7 @@ EOF
     pop(@vals); # The buffer ends "\0" so there's always a trailing empty value
                 # at the end of the buffer. I love perl! Perl is my friend!

-    die "$php_path: $f: bad option output from subprocess" if (scalar(@vals) % 2);
+    throw Error::Simple "$php_path: $f: bad option output from subprocess" if (scalar(@vals) % 2);

     my %config = @vals;

@@ -163,9 +163,9 @@ EOF

     if ($?) {
         if ($? & 127) {
-            die "$php_path: killed by signal " . ($? & 127);
+            throw Error::Simple "$php_path: killed by signal " . ($? & 127);
         } else {
-            die "$php_path: exited with failure status " . ($? >> 8);
+            throw Error::Simple "$php_path: exited with failure status " . ($? >> 8);
         }
     }

@@ -199,7 +199,7 @@ function is implicitly called by get and
 my %cached_configs;
 sub load_default() {
     my $filename = $main_config_filename;
-    die "Please call mySociety::Config::set_file to specify config file" if (!defined($filename));
+    throw Error::Simple "Please call mySociety::Config::set_file to specify config file" if (!defined($filename));

     if (!defined($cached_configs{$filename})) {
         $cached_configs{$filename} = read_config($filename);
@@ -224,7 +224,7 @@ sub get ($;$) {
     } elsif (@_ == 2) {
         return $default;
     } else {
-        die "No value for '$key' in '" . $config->{'CONFIG_FILE_NAME'} .  "', and no default specified";
+        throw Error::Simple "No value for '$key' in '" . $config->{'CONFIG_FILE_NAME'} .  "', and no default specified";
     }
 }



More information about the fiksgatami mailing list