Index: trunk/ippScripts/scripts/phase2.pl
===================================================================
--- trunk/ippScripts/scripts/phase2.pl	(revision 11298)
+++ trunk/ippScripts/scripts/phase2.pl	(revision 11316)
@@ -8,5 +8,13 @@
 use PS::IPP::Metadata::Stats;
 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;
@@ -51,5 +59,8 @@
 my $p2tool = can_run('p2tool') or (warn "Can't find p2tool" and $missing_tools = 1);
 my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-die "Can't find required tools.\n" if $missing_tools;
+if ($missing_tools) { 
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR); 
+}
 
 unless (defined $workdir) {
@@ -79,9 +90,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppImage on $input: $error_code\n" if not $success;
-    die "Couldn't find expected output file: $outputImage\n" if not -f $outputImage;
-    die "Couldn't find expected output file: $outputBin1\n" if not -f $outputBin1;
-    die "Couldn't find expected output file: $outputBin2\n" if not -f $outputBin2;
-    die "Couldn't find expected output file: $outputStats\n" if not -f $outputStats;
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform ppImage: $error_code", $exp_tag, $class_id, $error_code);
+    }
+    &my_die("Couldn't find expected output file: $outputImage\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputImage;
+    &my_die("Couldn't find expected output file: $outputBin1\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin1;
+    &my_die("Couldn't find expected output file: $outputBin2\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
+    &my_die("Couldn't find expected output file: $outputStats\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
 }
 
@@ -90,12 +104,13 @@
 {
     my $statsFile;		# File handle
-    open $statsFile, $outputStats or die "Can't open statistics file $outputStats: $!\n";
+    open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR);
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
-    my $metadata = $mdcParser->parse(join "", @contents)
-            or die "unable to parse metadata config doc";
+    my $metadata = $mdcParser->parse(join "", @contents) or
+	&my_die("Unable to parse metadata config doc", $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
+    $stats->parse($metadata) or
+	&my_die("Unable to find all values in statistics output.\n", $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -120,8 +135,28 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform p2tool -addprocessedimfile for $expTag/$classId: $error_code\n"
-	if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform p2tool -addprocessedimfile: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $outputStats;
+}
+
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $exp_tag = shift; # Exposure tag
+    my $class_id = shift; # Class identifier
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($exp_tag and $class_id) {
+	my $command = "$dettool -addprocessedimfile -exp_tag $exp_tag -class_id $class_id -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
