Fix InvalidInput wrong message

When performing some operations, there is a chance that the API
will raise a InvalidInput exception. However, sometimes we are
treating this exception alongside others, and expecting these
exceptions to inherit from the same base. The exceptions could
have different attributes to store the message though.

This change modifies the message treatment to stringify the
exception instead of trying to use specific attributes.

Change-Id: I1f54f6be46c5a1e997c8bf589503a5e62a71d644
This commit is contained in:
silvacarloss 2022-07-14 18:28:53 -03:00
parent 193784308c
commit 51bc158305
3 changed files with 20 additions and 3 deletions

View File

@ -554,7 +554,7 @@ class ShareMixin(object):
try:
self.share_api.extend(context, share, size, force=force)
except (exception.InvalidInput, exception.InvalidShare) as e:
raise webob.exc.HTTPBadRequest(explanation=e.message)
raise webob.exc.HTTPBadRequest(explanation=str(e))
except exception.ShareSizeExceedsAvailableQuota as e:
raise webob.exc.HTTPForbidden(explanation=e.message)
@ -574,7 +574,7 @@ class ShareMixin(object):
try:
self.share_api.shrink(context, share, size)
except (exception.InvalidInput, exception.InvalidShare) as e:
raise webob.exc.HTTPBadRequest(explanation=e.message)
raise webob.exc.HTTPBadRequest(explanation=str(e))
return webob.Response(status_int=http_client.ACCEPTED)

View File

@ -280,7 +280,7 @@ class ShareGroupController(wsgi.Controller, wsgi.AdminActionsMixin):
raise exc.HTTPConflict(explanation=e.message)
except (exception.ShareGroupSnapshotNotFound,
exception.InvalidInput) as e:
raise exc.HTTPBadRequest(explanation=e.message)
raise exc.HTTPBadRequest(explanation=str(e))
return self._view_builder.detail(
req, {k: v for k, v in new_share_group.items()})

View File

@ -571,6 +571,23 @@ class ShareGroupAPITest(test.TestCase):
self.mock_policy_check.assert_called_once_with(
self.context, self.resource_name, 'create')
def test_share_group_create_invalid_input(self):
fake_snap_id = uuidutils.generate_uuid()
body = {
"share_group": {"source_share_group_snapshot_id": fake_snap_id}
}
self.mock_object(
self.controller.share_group_api, 'create',
mock.Mock(side_effect=exception.InvalidInput(
reason='invalid input')))
self.assertRaises(
webob.exc.HTTPBadRequest,
self.controller.create, self.request, body)
self.mock_policy_check.assert_called_once_with(
self.context, self.resource_name, 'create')
def test_share_group_create_source_group_snapshot_not_a_uuid(self):
fake_snap_id = "Not a uuid"
body = {