catch the correct exception for alarm not found case

Issue:
for the alarm_show/alarm_delete/event_show api, if the input UUID
string is not valid, http server side will return error code 500
(Internal Server Error), due to the error code is not set correctly.
Solution:
Correct the error code in server, and add client code to handle
"HTTP 404 - Not Found" error code correctly.

Test:
Run "fm alarm-show/alarm-delete/event-show" with valid/invalid
UUID, and the response is correct. For invalid UUID, response
string will like below. "xxx" for the invalid UUID string.
"Alarm not found: xxx"
"Event log not found: xxx"

Closes-Bug: 1806927

Change-Id: I8d17c5bc55733f269d875ae835ab6295fed4d899
Signed-off-by: Shuicheng Lin <shuicheng.lin@intel.com>
This commit is contained in:
Shuicheng Lin 2019-03-13 00:57:54 +08:00
parent c6b9accb4f
commit 1faff551f2
6 changed files with 28 additions and 10 deletions

View File

@ -1,2 +1,2 @@
SRC_DIR="fm" SRC_DIR="fm"
TIS_PATCH_VER=2 TIS_PATCH_VER=3

View File

@ -16,7 +16,8 @@ class ApiError(Exception):
message = _("An unknown exception occurred.") message = _("An unknown exception occurred.")
code = webob.exc.HTTPInternalServerError # 500 - HTTPInternalServerError
code = webob.exc.HTTPInternalServerError.code
def __init__(self, message=None, **kwargs): def __init__(self, message=None, **kwargs):
@ -51,7 +52,8 @@ class ApiError(Exception):
class NotFound(ApiError): class NotFound(ApiError):
message = _("Resource could not be found.") message = _("Resource could not be found.")
code = webob.exc.HTTPNotFound # 404 - HTTPNotFound
code = webob.exc.HTTPNotFound.code
class HTTPNotFound(NotFound): class HTTPNotFound(NotFound):
@ -76,7 +78,8 @@ class ServerNotFound(NotFound):
class Invalid(ApiError): class Invalid(ApiError):
message = _("Unacceptable parameters.") message = _("Unacceptable parameters.")
code = webob.exc.HTTPBadRequest # 400 - HTTPBadRequest
code = webob.exc.HTTPBadRequest.code
class PatchError(Invalid): class PatchError(Invalid):
@ -97,12 +100,14 @@ class InvalidIdentity(Invalid):
class PolicyNotAuthorized(ApiError): class PolicyNotAuthorized(ApiError):
message = _("Policy doesn't allow %(action)s to be performed.") message = _("Policy doesn't allow %(action)s to be performed.")
code = webob.exc.HTTPUnauthorized # 401 - HTTPUnauthorized
code = webob.exc.HTTPUnauthorized.code
class Conflict(ApiError): class Conflict(ApiError):
message = _('HTTP Conflict.') message = _('HTTP Conflict.')
code = webob.exc.HTTPConflict # 409 - HTTPConflict
code = webob.exc.HTTPConflict.code
class AlarmAlreadyExists(Conflict): class AlarmAlreadyExists(Conflict):

View File

@ -1,2 +1,2 @@
SRC_DIR="fmclient" SRC_DIR="fmclient"
TIS_PATCH_VER=2 TIS_PATCH_VER=3

View File

@ -85,6 +85,15 @@ class HTTPClientError(HttpError):
message = _("HTTP Client Error") message = _("HTTP Client Error")
class HTTPNotFound(HTTPClientError):
"""HTTP 404 - Not Found
Exception for cases in which the server did not find anything
matching the Request-URI.
"""
message = _("HTTP Client Error: Not Found")
class HttpServerError(HttpError): class HttpServerError(HttpError):
"""Server-side HTTP error. """Server-side HTTP error.
@ -163,6 +172,8 @@ def from_response(response, method, url=None):
except KeyError: except KeyError:
if 500 <= response.status_code < 600: if 500 <= response.status_code < 600:
cls = HttpServerError cls = HttpServerError
elif 404 == response.status_code:
cls = HTTPNotFound
elif 400 <= response.status_code < 500: elif 400 <= response.status_code < 500:
cls = HTTPClientError cls = HTTPClientError
else: else:

View File

@ -6,6 +6,7 @@
from fmclient import exc from fmclient import exc
from fmclient.common import exceptions as exc_common
from fmclient.common import utils from fmclient.common import utils
from fmclient.common import wrapping_formatters from fmclient.common import wrapping_formatters
from fmclient.common import options from fmclient.common import options
@ -26,7 +27,7 @@ def do_alarm_show(cc, args={}):
'''Show an active alarm.''' '''Show an active alarm.'''
try: try:
fault = cc.alarm.get(args.alarm) fault = cc.alarm.get(args.alarm)
except exc.HTTPNotFound: except exc_common.HTTPNotFound:
raise exc.CommandError('Alarm not found: %s' % args.alarm) raise exc.CommandError('Alarm not found: %s' % args.alarm)
else: else:
_display_fault(fault) _display_fault(fault)
@ -37,7 +38,7 @@ def do_alarm_delete(cc, args={}):
'''Delete an active alarm.''' '''Delete an active alarm.'''
try: try:
cc.alarm.delete(args.alarm) cc.alarm.delete(args.alarm)
except exc.HTTPNotFound: except exc_common.HTTPNotFound:
raise exc.CommandError('Alarm not found: %s' % args.alarm) raise exc.CommandError('Alarm not found: %s' % args.alarm)

View File

@ -6,6 +6,7 @@
from fmclient import exc from fmclient import exc
from fmclient.common import exceptions as exc_common
from fmclient.common import utils from fmclient.common import utils
from fmclient.common import wrapping_formatters from fmclient.common import wrapping_formatters
from fmclient.common import options from fmclient.common import options
@ -28,7 +29,7 @@ def do_event_show(cc, args={}):
'''Show a event log.''' '''Show a event log.'''
try: try:
log = cc.event_log.get(args.event_log) log = cc.event_log.get(args.event_log)
except exc.HTTPNotFound: except exc_common.HTTPNotFound:
raise exc.CommandError('Event log not found: %s' % args.event_log) raise exc.CommandError('Event log not found: %s' % args.event_log)
else: else:
_display_event(log) _display_event(log)