From 6c63ed75f73dc610e2e5c8edd0c8d8fd954b1936 Mon Sep 17 00:00:00 2001
From: Schankula Christopher <schankuc@mcmaster.ca>
Date: Wed, 7 Mar 2018 09:57:56 -0500
Subject: [PATCH] error handling on WormsAPI idToClassification

---
 src/biotree/BioTree.java     | 14 +++++++++-----
 src/biotree/TestBioTree.java |  1 +
 src/biotree/WormsAPI.java    |  1 +
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/biotree/BioTree.java b/src/biotree/BioTree.java
index 89a88b9..c5d6e93 100644
--- a/src/biotree/BioTree.java
+++ b/src/biotree/BioTree.java
@@ -45,9 +45,10 @@ public class BioTree {
 	 * @param taxonId The taxonId of the possible new entry
 	 * @return taxonId of new species entry
 	 */
-	public static int processRecord(int taxonId) {
+	public static Integer processRecord(int taxonId) {
 		//pass taxonId directly to function to add / increment it
-		processTaxonId(taxonId);
+		if (processTaxonId(taxonId)) return null;
+		System.out.println(taxonId);
 		return taxonId;
 	}
 	
@@ -59,10 +60,10 @@ public class BioTree {
 	 * @return taxonId of new / existing entry
 	 * @throws IOException 
 	 */
-	public static int processRecord(String scientificName) throws IOException {
+	public static Integer processRecord(String scientificName) throws IOException {
 		//reverse lookup based on name, try adding the found taxonId.
 		int taxonId = WormsAPI.nameToID(scientificName);
-		processTaxonId(taxonId);
+		if (processTaxonId(taxonId)) return null;
 		return taxonId;
 	}
 	
@@ -70,8 +71,9 @@ public class BioTree {
 	 * Process a new entry if it doesn't exist. If it does exist, increment the number
 	 * of Records for this classification by one. 
 	 * @param taxonId New / existing TaxonID to add / increment count thereof.
+	 * @return true if the process failed, false if nothing went wrong
 	 */
-	private static void processTaxonId(int taxonId) {
+	private static boolean processTaxonId(int taxonId) {
 		TaxonNode[] newNodes = null;			//possible eventual new nodes
 		TaxonNode tx = nodes.get(taxonId);	//search tree to see if the node exists already
 		if (tx != null)						//if it does exist, increment its count
@@ -83,6 +85,7 @@ public class BioTree {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
+			if (newNodes == null) return true;
 			newNodes[newNodes.length - 1].incCount();	//one of the new nodes exists
 			
 			for (int i = newNodes.length - 1; i >= 0; i--) {		//iterate over all node starting from lowest child
@@ -102,6 +105,7 @@ public class BioTree {
 					break;
 			}
 		}
+		return false;
 	}
 
 	/**
diff --git a/src/biotree/TestBioTree.java b/src/biotree/TestBioTree.java
index 6391b5f..67005ca 100644
--- a/src/biotree/TestBioTree.java
+++ b/src/biotree/TestBioTree.java
@@ -20,6 +20,7 @@ public class TestBioTree {
 		BioTree.processRecord(125122);
 		BioTree.processRecord(125392);
 		BioTree.processRecord(125391);
+		BioTree.processRecord(600000000);
 		//System.out.println(nodes.size());
 		//System.out.println(nodes.get(123207));
 		//Iterable<Integer> keys = nodes.keys();
diff --git a/src/biotree/WormsAPI.java b/src/biotree/WormsAPI.java
index 58bdc47..79c0cca 100644
--- a/src/biotree/WormsAPI.java
+++ b/src/biotree/WormsAPI.java
@@ -79,6 +79,7 @@ public class WormsAPI {
 	public static TaxonNode[] idToClassification(int taxonId) throws IOException, ParseException {
 		String resp = makeRequest(
 				String.format("http://marinespecies.org/rest/AphiaClassificationByAphiaID/%d", taxonId));
+		if (resp.length() == 0) return null;
 		JSONParser parser = new JSONParser();
 		JSONObject json = (JSONObject) parser.parse(resp);
 
-- 
GitLab