Index: trunk/ippScripts/scripts/phase0_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/phase0_imfile.pl	(revision 10625)
+++ trunk/ippScripts/scripts/phase0_imfile.pl	(revision 11036)
@@ -15,5 +15,14 @@
 use Data::Dumper;
 
-use PS::IPP::Config;
+use PS::IPP::Config qw(
+    $PS_EXIT_SUCCESS
+    $PS_EXIT_UNKNOWN_ERROR
+    $PS_EXIT_SYS_ERROR
+    $PS_EXIT_CONFIG_ERROR
+    $PS_EXIT_PROG_ERROR
+    $PS_EXIT_DATA_ERROR
+    $PS_EXIT_TIMEOUT_ERROR
+    );
+
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
 use File::Spec;
@@ -40,5 +49,6 @@
     and defined $uri;
 
-use constant RECIPE => "PPSTATS_PHASE0"; # Recipe to use for ppStats
+my $RECIPE = "PPSTATS_PHASE0"; # Recipe to use for ppStats
+# use constant RECIPE => "PPSTATS_PHASE0"; # Recipe to use for ppStats
 
 # These values should be constant for all components
@@ -77,5 +87,9 @@
 my $p0tool = can_run('p0tool') or (warn "Can't find p0tool" and $missing_tools = 1);
 my $ppStats = can_run('ppStats') or (warn "Can't find ppStats" and $missing_tools = 1);
-die "Can't find required tools.\n" if $missing_tools;
+
+if ($missing_tools) { 
+    warn ("Can't find required tools");
+    &my_die ("", "", $PS_EXIT_CONFIG_ERROR); 
+}
 
 # setup cache interface
@@ -92,18 +106,34 @@
 my $stats;
 {
-    my $command = "$ppStats $uri -recipe PPSTATS " . RECIPE; # Command to run ppStats
+    my $command = "$ppStats $uri -recipe PPSTATS $RECIPE"; # Command to run ppStats
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         cache_run(command => $command, verbose => 1);
-    die "Unable to perform ppStats on exposure id $exp_tag: $error_code\n"
-        if not $success;
+    unless ($success) { 
+	warn ("Unable to perform ppStats on exposure id $exp_tag: $error_code");
+	&my_die ($exp_tag, $class_id, $error_code);
+    }
     
     # Parse the output
     my $mdcParser = PS::IPP::Metadata::Config->new;        # Parser for metadata config files
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf)
-            or die "unable to parse metadata config doc";
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf); # XXX is this join necessary?
+    unless ($metadata) {
+	warn ("Unable to parse metadata config doc");
+	&my_die ($exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+    }
     my @constants = keys %{CONSTANTS()}; # List of constants to parse out
     my @variables = keys %{VARIABLES()}; # List of variables to parse out
     $stats = PS::IPP::Metadata::Stats->new(\@constants, \@variables); # Stats parser
-    $stats->parse($metadata) or die "Unable to find all values.\n";
+    unless ($stats->parse($metadata)) {
+	warn ("Unable to find all values");
+	&my_die ($exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+	# XXX is this a programming or a config error?
+    }
+ 
+    # XXX for a test, randomly declare a failure and return to pantasks
+    my $rnd = rand(1);
+    if ($rnd > 0.5) {
+	warn ("random failure");
+	&my_die ($exp_tag, $class_id, $PS_EXIT_DATA_ERROR);
+    }
 }
 
@@ -151,5 +181,11 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => \@command, verbose => 1);
-    die "Unable to perform p0tool -updateimfile: $error_code\n" if not $success;
+
+    unless ($success) {
+	# XXX this is tricky: if we can't run -updateimfile, can we actually set the error code?
+	# XXX if this is not a database error, it is probably a programming error (in p0tool or the passed args)
+	warn ("Unable to perform p0tool -updateimfile: $error_code");
+	&my_die ($exp_tag, $class_id, $error_code);
+    }
 }
 
@@ -166,4 +202,15 @@
         return @output;
     }
+}
+
+sub my_die
+{
+    my $exp_tag = $_[0];
+    my $class_id = $_[1];
+    my $exit_code = $_[2];
+    if ($exp_tag && $class_id) {
+	system ("$p0tool -faultimfile -exp_tag $exp_tag -class_id $class_id -code $exit_code");
+    }
+    exit $exit_code;
 }
 
