Index: trunk/ippScripts/scripts/phase0_exp.pl
===================================================================
--- trunk/ippScripts/scripts/phase0_exp.pl	(revision 10625)
+++ trunk/ippScripts/scripts/phase0_exp.pl	(revision 11036)
@@ -14,4 +14,14 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+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 ($cache, $exptag, $no_update);
@@ -67,5 +77,8 @@
 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
@@ -84,7 +97,14 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         cache_run(command => $command, verbose => 1);
-    die "Unable to perform p0tool on exposure id $exptag: $error_code\n" if not $success;
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf)
-            or die "unable to parse metadata config doc";
+    unless ($success) {
+	warn ("Unable to perform p0tool on exposure id $exptag: $error_code");
+	&my_die ($exptag, $error_code);
+    }
+
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf);
+    unless ($metadata) {
+	warn ("Unable to parse metadata config doc");
+	&my_die ($exptag, $PS_EXIT_PROG_ERROR);
+    }
     $imfiles = parse_md_list($metadata); # Data for imfiles
 }
@@ -104,7 +124,8 @@
         if (not defined $values{$constant}) {
             $values{$constant} = $value;
-        } elsif ($values{$constant} ne $value) {
-            die "Value of $constant for " . $imfile->{PHASE0_CLASSID} .
-                " doesn't match previous value.\n";
+        }
+	if ($values{$constant} ne $value) {
+            warn ("Value of $constant for $imfile->{PHASE0_CLASSID} doesn't match previous value");
+	    &my_die ($exptag, $PS_EXIT_PROG_ERROR);
         }
     }
@@ -121,4 +142,11 @@
     my $stdev = get_value($imfile, PHASE0_BG_MEAN_STDEV());
     push @variances, $stdev**2;
+}
+
+# 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 ($exptag, $PS_EXIT_DATA_ERROR);
 }
 
@@ -169,9 +197,11 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         cache_run(command => \@command, verbose => 1);
-    die "Unable to run phase0 update for $exptag: $error_code\n" if not $success;
+    unless ($success) {
+	warn ("Unable to run phase0 update for $exptag: $error_code");
+	&my_die ($exptag, $error_code);
+    }
 }
 
 ### Pau.
-
 
 # Get the value for a particular imfile, along with a strong check for its existence
@@ -181,5 +211,9 @@
 
     my $source = $imfile->{PHASE0_CLASSID()}; # Where it comes from
-    die "Couldn't find value of $name for class_id=$source\n" if not defined $imfile->{$name};
+
+    unless (defined $imfile->{$name}) {
+	warn ("Couldn't find value of $name for class_id=$source");
+	&my_die ($exptag, $PS_EXIT_PROG_ERROR);
+    }
     return $imfile->{$name};
 }
@@ -199,4 +233,14 @@
 }
 
+sub my_die
+{
+    my $exp_tag = $_[0];
+    my $exit_code = $_[1];
+    if ($exp_tag) {
+	system ("$p0tool -faultexp -exp_tag $exp_tag -code $exit_code");
+    }
+    exit $exit_code;
+}
+
 END {
     my $exit = $?;
@@ -204,3 +248,2 @@
     $? = $exit;
 }
-
