command/commit

Some more error handling

authorJan Wielemaker
Thu Mar 10 17:27:01 2011 +0100
committerJan Wielemaker
Thu Mar 10 17:28:58 2011 +0100
commitd52ac26e3389968d2b8d6f86f7da1ac5bee6f517
tree8e54813d7ee0331b120819d823f515e71443a23b
parente6dd4baf9bbab111516642c55ce0457940253f14
Diff style: patch stat
diff --git a/script/cp-client.c b/script/cp-client.c
index 4d36da4..1816d41 100644
--- a/script/cp-client.c
+++ b/script/cp-client.c
@@ -84,7 +84,7 @@ usage(int code)
   fprintf(stderr, "  load file|url    Load data from file or URL\n");
   fprintf(stderr, "  sparql query     Run a SPARQL query\n");
 
-  exit(code);
+  return code;
 }
 
 typedef enum argtype
@@ -140,6 +140,23 @@ cmd_url(const char *path, ...)
 
 
 
+static int
+curl_status(CURLcode rc)
+{ long http_code;
+
+  if ( rc )
+    fprintf(stderr, "%s: %s\n", program, curl_easy_strerror(rc));
+
+  rc = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
+  if ( debug )
+    fprintf(stderr, "HTTP CODE: %ld\n", http_code);
+  if ( http_code != 200 )
+    rc = http_code / 100;		/* system return codes is a byte */
+
+  return rc;
+}
+
+
 		 /*******************************
 		 *	      PING		*
 		 *******************************/
@@ -151,10 +168,7 @@ ping()
   curl_easy_setopt(curl, CURLOPT_URL, cmd_url("/cmd/ping", A_END));
   rc = curl_easy_perform(curl);
 
-  if ( rc )
-    fprintf(stderr, "%s: %s\n", program, curl_easy_strerror(rc));
-
-  return rc;
+  return curl_status(rc);
 }
 
 
@@ -172,10 +186,7 @@ load_one(const char *data)
   curl_easy_setopt(curl, CURLOPT_URL, url);
   rc = curl_easy_perform(curl);
 
-  if ( rc )
-    fprintf(stderr, "%s: %s\n", program, curl_easy_strerror(rc));
-
-  return rc;
+  return curl_status(rc);
 }
 
 
@@ -204,10 +215,7 @@ sparql(const char *query)
   curl_easy_setopt(curl, CURLOPT_URL, url);
   rc = curl_easy_perform(curl);
 
-  if ( rc )
-    fprintf(stderr, "%s: %s\n", program, curl_easy_strerror(rc));
-
-  return rc;
+  return curl_status(rc);
 }
 
 
@@ -239,13 +247,13 @@ main(int argc, char **argv)
     else if ( isoption(s, "netrc", &val) )
       netrc = (char*)val;
     else if ( s[0] == '-' )
-      usage(1);
+      return usage(1);
     else
       break;
   }
 
   if ( (cmdac = argc-i) == 0 )
-    usage(1);
+    return usage(1);
   cmdav = (const char**) &argv[i];
   command = cmdav[0];
   cmdav++;
@@ -271,10 +279,10 @@ main(int argc, char **argv)
   else if ( strcmp(command, "sparql") == 0 && cmdac == 1 )
     rc = sparql(cmdav[0]);
   else
-    usage(1);
+    rc = usage(1);
 
   curl_easy_cleanup(curl);
   curl = NULL;
 
-  return 0;
+  return rc;
 }