Catch more errors in case we're fed garbage through API calls

Don't hang or crash, that's bad.

Change-Id: I77114c20fe2431718eda8dbefde1c390fe8283d4
diff --git a/db/db.py b/db/db.py
index 3ba21a6..527e5ec 100644
--- a/db/db.py
+++ b/db/db.py
@@ -321,6 +321,9 @@
     # a quick wrapper
     def _get_element(self, select_field, table, compare_field, value):
 
+        if value is None:
+            raise ValueError("Asked to look up using None as a key in %s" % compare_field)
+
         # We really want to use psycopg's type handling deal with the
         # (potentially) user-supplied data in the value field, so we
         # have to pass (sql,data) through to cursor.execute. However,
@@ -346,6 +349,11 @@
     # a quick wrapper
     def _get_element2(self, select_field, table, compare_field1, value1, compare_field2, value2):
 
+        if value1 is None:
+            raise ValueError("Asked to look up using None as a key in %s" % compare_field1)
+        if value2 is None:
+            raise ValueError("Asked to look up using None as a key in %s" % compare_field2)
+
         # We really want to use psycopg's type handling deal with the
         # (potentially) user-supplied data in the value field, so we
         # have to pass (sql,data) through to cursor.execute. However,
@@ -367,6 +375,9 @@
     # wrapper
     def _get_multi_elements(self, select_field, table, compare_field, value, sort_field):
 
+        if value is None:
+            raise ValueError("Asked to look up using None as a key in %s" % compare_field)
+
         # We really want to use psycopg's type handling deal with the
         # (potentially) user-supplied data in the value field, so we
         # have to pass (sql,data) through to cursor.execute. However,
@@ -395,6 +406,11 @@
     # a wrapper
     def _get_multi_elements2(self, select_field, table, compare_field1, value1, compare_field2, value2, sort_field):
 
+        if value1 is None:
+            raise ValueError("Asked to look up using None as a key in %s" % compare_field1)
+        if value2 is None:
+            raise ValueError("Asked to look up using None as a key in %s" % compare_field2)
+
         # We really want to use psycopg's type handling deal with the
         # (potentially) user-supplied data in the value field, so we
         # have to pass (sql,data) through to cursor.execute. However,
diff --git a/util.py b/util.py
index c110380..090e06e 100644
--- a/util.py
+++ b/util.py
@@ -124,6 +124,9 @@
         except ValueError as e:
             logging.error('perform_db_query(%s) got error %s', command, e)
             raise InputError("Invalid value in API call argument: %s" % e)
+        except TypeError as e:
+            logging.error('perform_db_query(%s) got error %s', command, e)
+            raise InputError("Invalid type in API call argument: %s" % e)
 
         return ret
 
@@ -157,7 +160,10 @@
             raise
         except ValueError as e:
             logging.error('perform_daemon_query(%s) got error %s', command, e)
-            raise InputError("Invalid value in API call: %s" % e)
+            raise InputError("Invalid value in API call argument: %s" % e)
+        except TypeError as e:
+            logging.error('perform_daemon_query(%s) got error %s', command, e)
+            raise InputError("Invalid type in API call argument: %s" % e)
 
         return ret
 
@@ -200,7 +206,10 @@
             raise
         except ValueError as e:
             logging.error('perform_db_update(%s) got error %s', command, e)
-            raise InputError("Invalid value in API call: %s" % e)
+            raise InputError("Invalid value in API call argument: %s" % e)
+        except TypeError as e:
+            logging.error('perform_db_update(%s) got error %s', command, e)
+            raise InputError("Invalid type in API call argument: %s" % e)
 
         return ret
 
@@ -235,7 +244,10 @@
             raise
         except ValueError as e:
             logging.error('perform_vlan_update(%s) got error %s', command, e)
-            raise InputError("Invalid value in API call: %s" % e)
+            raise InputError("Invalid value in API call argument: %s" % e)
+        except TypeError as e:
+            logging.error('perform_vlan_update(%s) got error %s', command, e)
+            raise InputError("Invalid type in API call argument: %s" % e)
 
         return ret