Fix usage of NotFound exception

All built-in inheritors of osc_lib.exceptions.ClientException expects
3 positional arguments: first argument is code, the second one is
message.

osc_lib.api.api.BaseAPI raises NotFound exception with transmitting only
message arguments which goes to position of 'code' argument.

This patch adds `code=404` argument and passes actual message as
'message'.

Also, this patch adds a hack that processes code as message
 n case when code is not an integer and None was found at 'message'
argument.

Funny fact:
More than 4 years ago, I proposed a similar fix to oslo-incubator... it
was so long ago...
https://review.opendev.org/#/c/94884 :)

Change-Id: I022bbf3ce4bdd48b3bdcb008ee872c4f62a7b12b
This commit is contained in:
Andrey Kurilin 2019-08-08 15:45:10 +03:00
parent e78db3b892
commit 2d4f3da090
2 changed files with 5 additions and 2 deletions

View File

@ -361,7 +361,7 @@ class BaseAPI(object):
num_bulk = len(bulk_list)
if num_bulk == 0:
msg = _("none found")
raise exceptions.NotFound(msg)
raise exceptions.NotFound(404, msg)
elif num_bulk > 1:
msg = _("many found")
raise RuntimeError(msg)
@ -388,7 +388,7 @@ class BaseAPI(object):
def raise_not_found():
msg = _("%s not found") % value
raise exceptions.NotFound(msg)
raise exceptions.NotFound(404, msg)
try:
ret = self._request(

View File

@ -55,6 +55,9 @@ class ClientException(Exception):
"""The base exception class for all exceptions this library raises."""
def __init__(self, code, message=None, details=None):
if not isinstance(code, int) and message is None:
message = code
code = self.http_status
self.code = code
self.message = message or self.__class__.message
self.details = details