aboutsummaryrefslogtreecommitdiff
path: root/util.py
diff options
context:
space:
mode:
authorSteve McIntyre <steve.mcintyre@linaro.org>2016-03-22 17:02:39 +0000
committerSteve McIntyre <steve.mcintyre@linaro.org>2016-03-22 17:02:39 +0000
commitb01959fb439d45b9cd631b80736fe4a9021b1037 (patch)
tree58d50f6199712786e9a1f0602170e888ed22caf4 /util.py
parent709f910210e74c64b07bfaef396080aaa6df8454 (diff)
Add better distinction of error cases
Add a new NotFoundError exception for use internally, so we can track specific failure cases. Also add a new Error class as a central place to store our error numbers consistently. In the API protocol, add a "NOTFOUND" error string alongside "ERROR" to help the admin interface and other callers distinguish error cases better. In the admin interface, actually return distinct non-zero errors in failure cases. Previously, almost all failures would have returned suceesfully to the calling shell. Change-Id: Ie382b737a80b7cd41c551e3a4a2a7e0827260bdc
Diffstat (limited to 'util.py')
-rw-r--r--util.py26
1 files changed, 13 insertions, 13 deletions
diff --git a/util.py b/util.py
index 25d8d9b..c110380 100644
--- a/util.py
+++ b/util.py
@@ -20,7 +20,7 @@
import logging
import time
-from errors import CriticalError, InputError, ConfigError, SocketError
+from errors import CriticalError, NotFoundError, InputError, ConfigError, SocketError
class VlanUtil:
"""VLANd utility functions"""
@@ -118,7 +118,7 @@ class VlanUtil:
else:
raise InputError("Unknown db_query command \"%s\"" % command)
- except InputError as e:
+ except (InputError, NotFoundError) as e:
logging.error('perform_db_query(%s) got error %s', command, e)
raise
except ValueError as e:
@@ -152,7 +152,7 @@ class VlanUtil:
else:
raise InputError("Unknown daemon_query command \"%s\"" % command)
- except InputError as e:
+ except (InputError, NotFoundError) as e:
logging.error('perform_daemon_query(%s) got error %s', command, e)
raise
except ValueError as e:
@@ -195,7 +195,7 @@ class VlanUtil:
else:
raise InputError("Unknown db_update command \"%s\"" % command)
- except InputError as e:
+ except (InputError, NotFoundError) as e:
logging.error('perform_db_update(%s) got error %s', command, e)
raise
except ValueError as e:
@@ -230,7 +230,7 @@ class VlanUtil:
else:
raise InputError("Unknown query command \"%s\"" % command)
- except InputError as e:
+ except (InputError, NotFoundError) as e:
logging.error('perform_vlan_update(%s) got error %s', command, e)
raise
except ValueError as e:
@@ -266,7 +266,7 @@ class VlanUtil:
logging.debug('Adding DB record first: name %s, tag %d, is_base_vlan %d', name, tag, is_base_vlan)
vlan_id = db.create_vlan(name, tag, is_base_vlan)
logging.debug('Added VLAN tag %d, name %s to the database, created VLAN ID %d', tag, name, vlan_id)
- except InputError:
+ except (InputError, NotFoundError):
logging.debug('DB creation failed')
raise
@@ -371,7 +371,7 @@ class VlanUtil:
logging.debug('Checking for ports using VLAN ID %d', vlan_id)
vlan = db.get_vlan_by_id(vlan_id)
if vlan is None:
- raise InputError("VLAN ID %d does not exist" % vlan_id)
+ raise NotFoundError("VLAN ID %d does not exist" % vlan_id)
vlan_tag = vlan.tag
ports = db.get_ports_by_current_vlan(vlan_id)
if ports is not None:
@@ -461,7 +461,7 @@ class VlanUtil:
logging.debug('Removing DB record: VLAN ID %d', vlan_id)
vlan_id = db.delete_vlan(vlan_id)
logging.debug('Removed VLAN ID %d from the database OK', vlan_id)
- except InputError:
+ except (InputError, NotFoundError):
logging.debug('DB deletion failed')
raise
@@ -491,7 +491,7 @@ class VlanUtil:
raise InputError("Port mode '%s' is not a valid option: try 'access' or 'trunk'" % mode)
port = db.get_port_by_id(port_id)
if port is None:
- raise InputError("Port ID %d does not exist" % port_id)
+ raise NotFoundError("Port ID %d does not exist" % port_id)
if port.is_locked:
raise InputError("Port ID %d is locked" % port_id)
if mode == 'trunk' and port.is_trunk:
@@ -580,7 +580,7 @@ class VlanUtil:
# 1. Sanity checks!
port = db.get_port_by_id(port_id)
if port is None:
- raise InputError("Port ID %d does not exist" % port_id)
+ raise NotFoundError("Port ID %d does not exist" % port_id)
if port.is_locked:
raise InputError("Port ID %d is locked" % port_id)
if port.is_trunk:
@@ -588,7 +588,7 @@ class VlanUtil:
vlan = db.get_vlan_by_id(vlan_id)
if vlan is None:
- raise InputError("VLAN ID %d does not exist" % vlan_id)
+ raise NotFoundError("VLAN ID %d does not exist" % vlan_id)
# Get the right driver
switch_name = db.get_switch_name_by_id(port.switch_id)
@@ -640,7 +640,7 @@ class VlanUtil:
# 1. Sanity checks!
port = db.get_port_by_id(port_id)
if port is None:
- raise InputError("Port ID %d does not exist" % port_id)
+ raise NotFoundError("Port ID %d does not exist" % port_id)
if port.is_trunk:
raise InputError("Port ID %d is not an access port" % port_id)
if port.is_locked:
@@ -708,7 +708,7 @@ class VlanUtil:
raise InputError("Switch name %s already exists in the DB (ID %d)" % (switch_name, switch_id))
if not switch_name in config.switches:
- raise InputError("Switch name %s not defined in config" % switch_name)
+ raise NotFoundError("Switch name %s not defined in config" % switch_name)
# 2. Now start reading config from the switch
try: