Browse Source

Added more error catching to URL setting

spficklin 11 years ago
parent
commit
91466d9d6c

+ 35 - 2
tripal_feature/includes/tripal_feature.sync_features.inc

@@ -166,11 +166,33 @@ function tripal_feature_set_urls($na = NULL, $job = NULL) {
   $nodes = db_query($sql);  
   while ($node = db_fetch_object($nodes)) {
    
-    // remove any previous alias
+    // get the URL alias
     $src = "node/$node->nid";
     $dst = tripal_feature_get_feature_url($node, $url_alias);
+    if (!$dst) {
+      db_query('DEALLOCATE "del_url_alias_by_src"');
+      db_query('DEALLOCATE "ins_url_alias_nisrds"');
+      db_query("ROLLBACK"); 
+      return; 
+    }    
+    
+    // remove any previous alias and then add the new one
     $success = db_query("EXECUTE del_url_alias_by_src('%s')", $src);    
+    if (!$success) {
+      db_query('DEALLOCATE "del_url_alias_by_src"');
+      db_query('DEALLOCATE "ins_url_alias_nisrds"');
+      db_query("ROLLBACK");
+      watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
+      return;
+    }
     $success = db_query("EXECUTE ins_url_alias_nisrds('%s', '%s')", $src, $dst);
+    if (!$success) {
+      db_query('DEALLOCATE "del_url_alias_by_src"');
+      db_query('DEALLOCATE "ins_url_alias_nisrds"');
+      db_query("ROLLBACK");
+      watchdog('trp-seturl', "Failed Adding URL Alias: %dst", array('%dst' => $dst), WATCHDOG_ERROR);
+      return;
+    }
 
     // update the job status every 1% features
     if ($job and $num_set % $num_per_interval == 0) {
@@ -220,13 +242,20 @@ function tripal_feature_get_feature_url($node, $url_alias = NULL) {
   $values = array('feature_id' => $node->feature_id); 
   $options = array('statement_name' => 'sel_feature_id');       
   $feature = tripal_core_chado_select('feature', array('*'), $values, $options);
-
+  if (!$feature) {
+    watchdog('trp-seturl', "Cannot find feature when setting URL alias for feature: %id", array('%id' => $node->feature_id), WATCHDOG_ERROR);
+    return FALSE;  
+  }
   $feature = (object) $feature[0];
   
   // get the organism
   $values = array('organism_id' => $feature->organism_id);
   $options = array('statement_name' => 'sel_organism_id');
   $organism  = tripal_core_chado_select('organism', array('*'), $values, $options);  
+  if (!$organism) {
+    watchdog('trp-seturl', "Cannot find organism when setting URL alias for feature: %id", array('%id' => $node->feature_id), WATCHDOG_ERROR);
+    return FALSE;  
+  }
   $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
   $species = preg_replace('/\s/', '_', strtolower($organism[0]->species)); 
 
@@ -234,6 +263,10 @@ function tripal_feature_get_feature_url($node, $url_alias = NULL) {
   $values = array('cvterm_id' => $feature->type_id);
   $options = array('statement_name' => 'sel_cvterm_id');
   $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
+  if (!$cvterm) {
+    watchdog('trp-seturl', "Cannot find type when setting URL alias for feature: %id", array('%id' => $node->feature_id), WATCHDOG_ERROR);
+    return FALSE;  
+  }
   $type = preg_replace('/\s/', '_', $cvterm[0]->name);
   
   // now substitute in the values

+ 36 - 3
tripal_stock/includes/tripal_stock.sync_stocks.inc

@@ -164,11 +164,33 @@ function tripal_stock_set_urls($na = NULL, $job = NULL) {
   $nodes = db_query($sql);  
   while ($node = db_fetch_object($nodes)) {
    
-    // remove any previous alias
+    // get the URL alias
     $src = "node/$node->nid";
     $dst = tripal_stock_get_stock_url($node, $url_alias);
+    if (!$dst) {
+      db_query('DEALLOCATE "del_url_alias_by_src"');
+      db_query('DEALLOCATE "ins_url_alias_nisrds"');
+      db_query("ROLLBACK"); 
+      return; 
+    }    
+    
+    // remove any previous alias and then add the new one
     $success = db_query("EXECUTE del_url_alias_by_src('%s')", $src);    
+    if (!$success) {
+      db_query('DEALLOCATE "del_url_alias_by_src"');
+      db_query('DEALLOCATE "ins_url_alias_nisrds"');
+      db_query("ROLLBACK");
+      watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
+      return;
+    }
     $success = db_query("EXECUTE ins_url_alias_nisrds('%s', '%s')", $src, $dst);
+    if (!$success) {
+      db_query('DEALLOCATE "del_url_alias_by_src"');
+      db_query('DEALLOCATE "ins_url_alias_nisrds"');
+      db_query("ROLLBACK");
+      watchdog('trp-seturl', "Failed Adding URL Alias: %dst", array('%dst' => $dst), WATCHDOG_ERROR);
+      return;
+    }
 
     // update the job status every 1% stocks
     if ($job and $num_set % $num_per_interval == 0) {
@@ -218,13 +240,20 @@ function tripal_stock_get_stock_url($node, $url_alias = NULL) {
   $values = array('stock_id' => $node->stock_id); 
   $options = array('statement_name' => 'sel_stock_id');       
   $stock = tripal_core_chado_select('stock', array('*'), $values, $options);
-
+  if (!$stock) {
+    watchdog('trp-seturl', "Cannot find stock when setting URL alias for stock: %id", array('%id' => $node->stock_id), WATCHDOG_ERROR);
+    return FALSE;  
+  }
   $stock = (object) $stock[0];
   
   // get the organism
   $values = array('organism_id' => $stock->organism_id);
   $options = array('statement_name' => 'sel_organism_id');
   $organism  = tripal_core_chado_select('organism', array('*'), $values, $options);  
+  if (!$organism) {
+    watchdog('trp-seturl', "Cannot find organism when setting URL alias for stock: %id", array('%id' => $node->stock_id), WATCHDOG_ERROR);
+    return FALSE;  
+  }
   $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
   $species = preg_replace('/\s/', '_', strtolower($organism[0]->species)); 
 
@@ -232,6 +261,10 @@ function tripal_stock_get_stock_url($node, $url_alias = NULL) {
   $values = array('cvterm_id' => $stock->type_id);
   $options = array('statement_name' => 'sel_cvterm_id');
   $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
+  if (!$cvterm) {
+    watchdog('trp-seturl', "Cannot find type when setting URL alias for stock: %id", array('%id' => $node->stock_id), WATCHDOG_ERROR);
+    return FALSE;  
+  }
   $type = preg_replace('/\s/', '_', $cvterm[0]->name);
   
   // now substitute in the values
@@ -475,7 +508,7 @@ global $user;
     if ($errors = form_get_errors()) {
       print "Error encountered validating new node. Cannot sync: $msg\n";
       foreach ($errors as $key => $msg) {        
-        watchdog('trp-fsync', "%msg", array('%msg' => $msg), 'error');
+        watchdog('trp-fsync', "%msg", array('%msg' => $msg), WATCHDOG_ERROR);
       }
       exit;
     }