Skip to content

Commit

Permalink
raise better exception for duplicate match
Browse files Browse the repository at this point in the history
When a duplicate match is found the client previously raised a
NeutronClientException which made it hard for programs using the client
to handle errors. This patch now  makes it raise a
NeutronClientNoUniqueMatch  exception instead.

Fixes bug: 1200323

Change-Id: I6ef6f9a9e257104f676dde7ec26be98417ec70e0
  • Loading branch information
aaronorosen committed Jul 16, 2013
1 parent 2fade1b commit 0417851
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
5 changes: 5 additions & 0 deletions neutronclient/common/exceptions.py
Expand Up @@ -167,3 +167,8 @@ class UnsupportedVersion(Exception):

class CommandError(Exception):
pass


class NeutronClientNoUniqueMatch(NeutronClientException):
message = _("Multiple %(resource)s matches found for name '%(name)s',"
" use an ID to be more specific.")
7 changes: 2 additions & 5 deletions neutronclient/neutron/v2_0/__init__.py
Expand Up @@ -52,11 +52,8 @@ def _find_resourceid_by_name(client, resource, name):
collection = resource + "s"
info = data[collection]
if len(info) > 1:
msg = (_("Multiple %(resource)s matches found for name '%(name)s',"
" use an ID to be more specific.") %
{'resource': resource, 'name': name})
raise exceptions.NeutronClientException(
message=msg)
raise exceptions.NeutronClientNoUniqueMatch(resource=resource,
name=name)
elif len(info) == 0:
not_found_message = (_("Unable to find %(resource)s with name "
"'%(name)s'") %
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_name_or_id.py
Expand Up @@ -109,7 +109,7 @@ def test_get_id_from_name_multiple(self):
try:
neutronV20.find_resourceid_by_name_or_id(
self.client, 'network', name)
except exceptions.NeutronClientException as ex:
except exceptions.NeutronClientNoUniqueMatch as ex:
self.assertTrue('Multiple' in ex.message)

def test_get_id_from_name_notfound(self):
Expand Down

0 comments on commit 0417851

Please sign in to comment.