Index: trunk/ippScripts/scripts/camera_exp.pl
===================================================================
--- trunk/ippScripts/scripts/camera_exp.pl	(revision 20661)
+++ trunk/ippScripts/scripts/camera_exp.pl	(revision 22430)
@@ -27,4 +27,17 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
+my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+my $psastro = can_run('psastro') or (warn "Can't find psastro" and $missing_tools = 1);
+my $addstar = can_run('addstar') or (warn "Can't find addstar" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ( $exp_tag, $cam_id, $camera, $outroot, $recipe, $dbname, $reduction, $dvodb, $verbose, $no_update,
@@ -57,4 +70,8 @@
     defined $camera;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $cam_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
@@ -84,20 +101,4 @@
 my $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR'); # Recipe to use
 &my_die("Unrecognised ADDSTAR recipe", $cam_id, $PS_EXIT_CONFIG_ERROR) unless defined $recipe1;
-
-# Look for programs we need
-my $missing_tools;
-my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
-my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-
-# test for addstar and psastro:
-my $psastro = can_run('psastro') or (warn "Can't find psastro" and $missing_tools = 1);
-my $addstar = can_run('addstar') or (warn "Can't find addstar" and $missing_tools = 1);
-
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
@@ -272,19 +273,19 @@
 
         if ($do_stats) {
-	    my $fpaStatsReal = $ipprc->file_resolve($fpaStats);
-	    &my_die("Couldn't find expected output file: $fpaStats", $cam_id, $PS_EXIT_SYS_ERROR) unless -f $fpaStatsReal;
-
-	    # parse stats from metadata
-	    $command = "$ppStatsFromMetadata $fpaStatsReal - CAMERA_EXP_FPA";
-	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-		run(command => $command, verbose => $verbose);
-	    unless ($success) {
-		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-		&my_die("Unable to perform ppStatsFromMetadata: $error_code", $cam_id, $error_code);
-	    }
-	    foreach my $line (@$stdout_buf) {
-		$cmdflags .= " $line";
-	    }
-	    chomp $cmdflags;
+            my $fpaStatsReal = $ipprc->file_resolve($fpaStats);
+            &my_die("Couldn't find expected output file: $fpaStats", $cam_id, $PS_EXIT_SYS_ERROR) unless -f $fpaStatsReal;
+
+            # parse stats from metadata
+            $command = "$ppStatsFromMetadata $fpaStatsReal - CAMERA_EXP_FPA";
+            ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+            unless ($success) {
+                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                &my_die("Unable to perform ppStatsFromMetadata: $error_code", $cam_id, $error_code);
+            }
+            foreach my $line (@$stdout_buf) {
+                $cmdflags .= " $line";
+            }
+            chomp $cmdflags;
         }
 
@@ -323,5 +324,5 @@
             $command .= " $realFile";
 
-	    my $mjd_addstar_start = DateTime->now->mjd;   # MJD of starting script
+            my $mjd_addstar_start = DateTime->now->mjd;   # MJD of starting script
 
             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -331,5 +332,5 @@
                 &my_die("Unable to perform addstar: $error_code", $cam_id, $error_code);
             }
-	    $dtime_addstar = 86400.0*(DateTime->now->mjd - $mjd_addstar_start);   # MJD of starting script
+            $dtime_addstar = 86400.0*(DateTime->now->mjd - $mjd_addstar_start);   # MJD of starting script
         }
     }
