From 1faff551f2865240a237a46fa8e2b4445a37a3cc Mon Sep 17 00:00:00 2001 From: Shuicheng Lin Date: Wed, 13 Mar 2019 00:57:54 +0800 Subject: [PATCH] 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 --- fm-rest-api/centos/build_srpm.data | 2 +- fm-rest-api/fm/fm/common/exceptions.py | 15 ++++++++++----- python-fmclient/centos/build_srpm.data | 2 +- .../fmclient/fmclient/common/exceptions.py | 11 +++++++++++ .../fmclient/fmclient/v1/alarm_shell.py | 5 +++-- .../fmclient/fmclient/v1/event_log_shell.py | 3 ++- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/fm-rest-api/centos/build_srpm.data b/fm-rest-api/centos/build_srpm.data index 917b7c11..1955a345 100644 --- a/fm-rest-api/centos/build_srpm.data +++ b/fm-rest-api/centos/build_srpm.data @@ -1,2 +1,2 @@ SRC_DIR="fm" -TIS_PATCH_VER=2 +TIS_PATCH_VER=3 diff --git a/fm-rest-api/fm/fm/common/exceptions.py b/fm-rest-api/fm/fm/common/exceptions.py index 5f7b910b..221aea52 100644 --- a/fm-rest-api/fm/fm/common/exceptions.py +++ b/fm-rest-api/fm/fm/common/exceptions.py @@ -16,7 +16,8 @@ class ApiError(Exception): message = _("An unknown exception occurred.") - code = webob.exc.HTTPInternalServerError + # 500 - HTTPInternalServerError + code = webob.exc.HTTPInternalServerError.code def __init__(self, message=None, **kwargs): @@ -51,7 +52,8 @@ class ApiError(Exception): class NotFound(ApiError): message = _("Resource could not be found.") - code = webob.exc.HTTPNotFound + # 404 - HTTPNotFound + code = webob.exc.HTTPNotFound.code class HTTPNotFound(NotFound): @@ -76,7 +78,8 @@ class ServerNotFound(NotFound): class Invalid(ApiError): message = _("Unacceptable parameters.") - code = webob.exc.HTTPBadRequest + # 400 - HTTPBadRequest + code = webob.exc.HTTPBadRequest.code class PatchError(Invalid): @@ -97,12 +100,14 @@ class InvalidIdentity(Invalid): class PolicyNotAuthorized(ApiError): message = _("Policy doesn't allow %(action)s to be performed.") - code = webob.exc.HTTPUnauthorized + # 401 - HTTPUnauthorized + code = webob.exc.HTTPUnauthorized.code class Conflict(ApiError): message = _('HTTP Conflict.') - code = webob.exc.HTTPConflict + # 409 - HTTPConflict + code = webob.exc.HTTPConflict.code class AlarmAlreadyExists(Conflict): diff --git a/python-fmclient/centos/build_srpm.data b/python-fmclient/centos/build_srpm.data index 58d7aee2..5915d0b8 100644 --- a/python-fmclient/centos/build_srpm.data +++ b/python-fmclient/centos/build_srpm.data @@ -1,2 +1,2 @@ SRC_DIR="fmclient" -TIS_PATCH_VER=2 +TIS_PATCH_VER=3 diff --git a/python-fmclient/fmclient/fmclient/common/exceptions.py b/python-fmclient/fmclient/fmclient/common/exceptions.py index 4c9578c5..46012b31 100644 --- a/python-fmclient/fmclient/fmclient/common/exceptions.py +++ b/python-fmclient/fmclient/fmclient/common/exceptions.py @@ -85,6 +85,15 @@ class HTTPClientError(HttpError): 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): """Server-side HTTP error. @@ -163,6 +172,8 @@ def from_response(response, method, url=None): except KeyError: if 500 <= response.status_code < 600: cls = HttpServerError + elif 404 == response.status_code: + cls = HTTPNotFound elif 400 <= response.status_code < 500: cls = HTTPClientError else: diff --git a/python-fmclient/fmclient/fmclient/v1/alarm_shell.py b/python-fmclient/fmclient/fmclient/v1/alarm_shell.py index e6e7e9e9..3d79d5d0 100755 --- a/python-fmclient/fmclient/fmclient/v1/alarm_shell.py +++ b/python-fmclient/fmclient/fmclient/v1/alarm_shell.py @@ -6,6 +6,7 @@ from fmclient import exc +from fmclient.common import exceptions as exc_common from fmclient.common import utils from fmclient.common import wrapping_formatters from fmclient.common import options @@ -26,7 +27,7 @@ def do_alarm_show(cc, args={}): '''Show an active alarm.''' try: fault = cc.alarm.get(args.alarm) - except exc.HTTPNotFound: + except exc_common.HTTPNotFound: raise exc.CommandError('Alarm not found: %s' % args.alarm) else: _display_fault(fault) @@ -37,7 +38,7 @@ def do_alarm_delete(cc, args={}): '''Delete an active alarm.''' try: cc.alarm.delete(args.alarm) - except exc.HTTPNotFound: + except exc_common.HTTPNotFound: raise exc.CommandError('Alarm not found: %s' % args.alarm) diff --git a/python-fmclient/fmclient/fmclient/v1/event_log_shell.py b/python-fmclient/fmclient/fmclient/v1/event_log_shell.py index 046186da..ce2db745 100644 --- a/python-fmclient/fmclient/fmclient/v1/event_log_shell.py +++ b/python-fmclient/fmclient/fmclient/v1/event_log_shell.py @@ -6,6 +6,7 @@ from fmclient import exc +from fmclient.common import exceptions as exc_common from fmclient.common import utils from fmclient.common import wrapping_formatters from fmclient.common import options @@ -28,7 +29,7 @@ def do_event_show(cc, args={}): '''Show a event log.''' try: 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) else: _display_event(log)