From 51bc15830594b0b7d07ccdc443ba8d32c19d7ca9 Mon Sep 17 00:00:00 2001 From: silvacarloss Date: Thu, 14 Jul 2022 18:28:53 -0300 Subject: [PATCH] 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 --- manila/api/v1/shares.py | 4 ++-- manila/api/v2/share_groups.py | 2 +- manila/tests/api/v2/test_share_groups.py | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/manila/api/v1/shares.py b/manila/api/v1/shares.py index bd74983096..8099b9a9fb 100644 --- a/manila/api/v1/shares.py +++ b/manila/api/v1/shares.py @@ -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) diff --git a/manila/api/v2/share_groups.py b/manila/api/v2/share_groups.py index fd728a5852..8da1a99398 100644 --- a/manila/api/v2/share_groups.py +++ b/manila/api/v2/share_groups.py @@ -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()}) diff --git a/manila/tests/api/v2/test_share_groups.py b/manila/tests/api/v2/test_share_groups.py index 48d4f83d9e..4dafcbc6df 100644 --- a/manila/tests/api/v2/test_share_groups.py +++ b/manila/tests/api/v2/test_share_groups.py @@ -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 = {