Index: trunk/pstamp/scripts/pstamp_parser_run.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 19221)
+++ trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 19265)
@@ -3,7 +3,7 @@
 ### pstampparser_run.pl
 ###     Run the request parser for a given request id
+### This script should be called request_parser.pl since it handles more than postage
+### stamp requests
 ###
-
-#XXX see notes about error handling
 
 use warnings;
@@ -172,24 +172,39 @@
     if (! -d $outProductDir);
 
-
-# run the appropriate parse command to parse the queue the jobs for this request
-# first check the extension header to find the EXTNAME
-my $request_type = find_request_type($uri);
-
-print STDERR "request_type for $req_id is $request_type\n" if $verbose;
-
 my $parse_cmd;
+my $request_type;
 my $reqType;    # for the database
-if ($request_type eq "PS1_PS_REQUEST") {
-    $reqType = 'pstamp';
-    $parse_cmd = $pstampparse;
-} elsif ($request_type eq "MOPS_DETECTABILITY_QUERY") {
-    $reqType = 'dquery';
-    $parse_cmd = $dqueryparse;
+
+if (-r $uri) {
+    # run the appropriate parse command to parse the queue the jobs for this request
+    # first check the extension header to find the EXTNAME
+    $request_type = find_request_type($uri);
+
+    if ($request_type) {
+        print STDERR "request_type for $req_id is $request_type\n" if $verbose;
+        if ($request_type eq "PS1_PS_REQUEST") {
+            $reqType = 'pstamp';
+            $parse_cmd = $pstampparse;
+        } elsif ($request_type eq "MOPS_DETECTABILITY_QUERY") {
+            $reqType = 'dquery';
+            $parse_cmd = $dqueryparse;
+        } else {
+            print STDERR "Unknown request type $request_type found in $uri";
+        }
+    } else {
+        print STDERR "No EXTNAME found keyword in $uri";
+    }
+} else {
+    if (-e $uri) {
+        print STDERR "Request file $uri is not readable";
+    } else {
+        print STDERR "Request file $uri does not exist";
+    }
 }
 
 if (!$parse_cmd) {
-    print STDERR "No EXTNAME found in $uri" if !$request_type;
-    print STDERR "Unknown request type $request_type found in $uri";
+    # can't go any farther, set fault and set request state to run 
+    # request_finish.pl will clean up, perhaps notifiying the operator of the input data store
+    # that they sent us a request file that we don't understand
 
     my $command = "$pstamptool -updatereq -req_id $req_id -state run";
@@ -211,5 +226,5 @@
 {
     my $error_file_name = "$workdir/parse_error.txt";
-    # get rid of any error file from previous run
+    # get rid of any error file from previous attempt to parse this request
     unlink $error_file_name if (-e $error_file_name);
 
@@ -219,5 +234,5 @@
 
     # save the contents of stderr (if any) to a file. This is relevant if
-    # the file parsed properl but one of the rows in the request file generated an error
+    # the file was parseable but one or more of the rows in the request file generated an error
     my $errbuf = join "", @$stderr_buf;
     if ($errbuf) {
@@ -258,13 +273,17 @@
 exit 0;
 
+
 sub find_request_type {
     # find the EXTNAME in the input fits table
-    # TODO: do this right handling errors etc.
     my $file_name = shift;
     my $out = `echo $file_name | fields -x 0 EXTNAME`;
 
-    # output from fields is filename value
-    my ($dummy, $extname) = split " ", $out;
-
-    return $extname;
-}
+    if ($out) {
+        # output from fields is filename value
+        my ($dummy, $extname) = split " ", $out;
+
+        return $extname;
+    } else {
+        return undef;
+    }
+}
