Fix exceptions handling for py34 compatibility

Fix following cases:

    "except SomeException, e:"
    "raise exc_value, msg, exc_trace"
    "e.message"

Partially-Implements: bp py3-compatibility

Change-Id: I8c20ae725000895b6b7b7c0da7d4bfba3e4849f0
This commit is contained in:
Valeriy Ponomaryov 2015-08-03 15:35:48 +03:00
parent f53613f5ff
commit 554b92e458
9 changed files with 52 additions and 43 deletions

View File

@ -769,7 +769,7 @@ class Resource(wsgi.Application):
try: try:
msg_dict = dict(url=request.url, status=response.status_int) msg_dict = dict(url=request.url, status=response.status_int)
msg = _("%(url)s returned with HTTP %(status)d") % msg_dict msg = _("%(url)s returned with HTTP %(status)d") % msg_dict
except AttributeError, e: except AttributeError as e:
msg_dict = dict(url=request.url, e=e) msg_dict = dict(url=request.url, e=e)
msg = _("%(url)s returned a fault: %(e)s") % msg_dict msg = _("%(url)s returned a fault: %(e)s") % msg_dict

View File

@ -16,8 +16,6 @@
Handles all requests to Nova. Handles all requests to Nova.
""" """
import sys
from novaclient import exceptions as nova_exception from novaclient import exceptions as nova_exception
from novaclient import service_catalog from novaclient import service_catalog
from novaclient import utils from novaclient import utils
@ -26,6 +24,7 @@ from novaclient.v2.contrib import assisted_volume_snapshots
from novaclient.v2 import servers as nova_servers from novaclient.v2 import servers as nova_servers
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log from oslo_log import log
import six
from manila.db import base from manila.db import base
from manila import exception from manila import exception
@ -152,13 +151,11 @@ def translate_server_exception(method):
def wrapper(self, ctx, instance_id, *args, **kwargs): def wrapper(self, ctx, instance_id, *args, **kwargs):
try: try:
res = method(self, ctx, instance_id, *args, **kwargs) res = method(self, ctx, instance_id, *args, **kwargs)
except nova_exception.ClientException: except nova_exception.ClientException as e:
exc_type, exc_value, exc_trace = sys.exc_info() if isinstance(e, nova_exception.NotFound):
if isinstance(exc_value, nova_exception.NotFound): raise exception.InstanceNotFound(instance_id=instance_id)
exc_value = exception.InstanceNotFound(instance_id=instance_id) elif isinstance(e, nova_exception.BadRequest):
elif isinstance(exc_value, nova_exception.BadRequest): raise exception.InvalidInput(reason=six.text_type(e))
exc_value = exception.InvalidInput(reason=exc_value.message)
raise exc_value, None, exc_trace
return res return res
return wrapper return wrapper

View File

@ -166,7 +166,7 @@ class API(base.Base):
try: try:
is_public = strutils.bool_from_string(is_public, strict=True) is_public = strutils.bool_from_string(is_public, strict=True)
except ValueError as e: except ValueError as e:
raise exception.InvalidParameterValue(e.message) raise exception.InvalidParameterValue(six.text_type(e))
options = {'size': size, options = {'size': size,
'user_id': context.user_id, 'user_id': context.user_id,
@ -432,7 +432,7 @@ class API(base.Base):
fields['is_public'] = strutils.bool_from_string( fields['is_public'] = strutils.bool_from_string(
fields['is_public'], strict=True) fields['is_public'], strict=True)
except ValueError as e: except ValueError as e:
raise exception.InvalidParameterValue(e.message) raise exception.InvalidParameterValue(six.text_type(e))
return self.db.share_update(context, share['id'], fields) return self.db.share_update(context, share['id'], fields)
@policy.wrap_check_policy('share') @policy.wrap_check_policy('share')

View File

@ -178,7 +178,7 @@ class GlusterManager(object):
raise exception.GlusterfsException( raise exception.GlusterfsException(
_("'gluster version' failed on server " _("'gluster version' failed on server "
"%(server)s: %(message)s") % "%(server)s: %(message)s") %
{'server': self.host, 'message': exc.message}) {'server': self.host, 'message': six.text_type(exc)})
try: try:
owords = out.split() owords = out.split()
if owords[0] != 'glusterfs': if owords[0] != 'glusterfs':

View File

@ -166,7 +166,7 @@ class GlusterfsNativeShareDriver(driver.ExecuteMixin, driver.ShareDriver):
try: try:
glusterfs_versions[srvaddr] = gluster_mgr.get_gluster_version() glusterfs_versions[srvaddr] = gluster_mgr.get_gluster_version()
except exception.GlusterfsException as exc: except exception.GlusterfsException as exc:
exceptions[srvaddr] = exc.message exceptions[srvaddr] = six.text_type(exc)
if exceptions: if exceptions:
for srvaddr, excmsg in six.iteritems(exceptions): for srvaddr, excmsg in six.iteritems(exceptions):
LOG.error(_LE("'gluster version' failed on server " LOG.error(_LE("'gluster version' failed on server "

View File

@ -21,6 +21,7 @@ import sys
import ddt import ddt
import mock import mock
from oslo_config import cfg from oslo_config import cfg
import six
from manila.cmd import manage as manila_manage from manila.cmd import manage as manila_manage
from manila import context from manila import context
@ -82,7 +83,7 @@ class ManilaCmdManageTestCase(test.TestCase):
try: try:
import bpython import bpython
except ImportError as e: except ImportError as e:
self.skipTest(e.message) self.skipTest(six.text_type(e))
self.mock_object(bpython, 'embed') self.mock_object(bpython, 'embed')
self.shell_commands.run(**kwargs) self.shell_commands.run(**kwargs)
bpython.embed.assert_called_once_with() bpython.embed.assert_called_once_with()
@ -92,7 +93,7 @@ class ManilaCmdManageTestCase(test.TestCase):
import bpython import bpython
import IPython import IPython
except ImportError as e: except ImportError as e:
self.skipTest(e.message) self.skipTest(six.text_type(e))
self.mock_object(bpython, 'embed', self.mock_object(bpython, 'embed',
mock.Mock(side_effect=ImportError())) mock.Mock(side_effect=ImportError()))
self.mock_object(IPython, 'embed') self.mock_object(IPython, 'embed')
@ -105,7 +106,7 @@ class ManilaCmdManageTestCase(test.TestCase):
try: try:
import bpython import bpython
except ImportError as e: except ImportError as e:
self.skipTest(e.message) self.skipTest(six.text_type(e))
self.mock_object(bpython, 'embed') self.mock_object(bpython, 'embed')
self.shell_commands.run() self.shell_commands.run()
@ -116,7 +117,7 @@ class ManilaCmdManageTestCase(test.TestCase):
try: try:
import IPython import IPython
except ImportError as e: except ImportError as e:
self.skipTest(e.message) self.skipTest(six.text_type(e))
self.mock_object(IPython, 'embed') self.mock_object(IPython, 'embed')
self.shell_commands.run(shell='ipython') self.shell_commands.run(shell='ipython')
@ -131,7 +132,7 @@ class ManilaCmdManageTestCase(test.TestCase):
setattr(IPython.Shell, 'IPShell', setattr(IPython.Shell, 'IPShell',
mock.Mock(side_effect=ImportError())) mock.Mock(side_effect=ImportError()))
except ImportError as e: except ImportError as e:
self.skipTest(e.message) self.skipTest(six.text_type(e))
self.mock_object(IPython, 'embed', self.mock_object(IPython, 'embed',
mock.Mock(side_effect=ImportError())) mock.Mock(side_effect=ImportError()))
self.mock_object(readline, 'parse_and_bind') self.mock_object(readline, 'parse_and_bind')

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import ddt
import mock import mock
from novaclient import exceptions as nova_exception from novaclient import exceptions as nova_exception
from novaclient import utils from novaclient import utils
@ -85,6 +86,7 @@ class FakeNovaClient(object):
self.fixed_ips = self.FixedIPs() self.fixed_ips = self.FixedIPs()
@ddt.ddt
class NovaApiTestCase(test.TestCase): class NovaApiTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(NovaApiTestCase, self).setUp() super(NovaApiTestCase, self).setUp()
@ -123,11 +125,17 @@ class NovaApiTestCase(test.TestCase):
self.assertEqual(instance_id, result['id']) self.assertEqual(instance_id, result['id'])
utils.find_resource.assert_called_once_with(mock.ANY, instance_id) utils.find_resource.assert_called_once_with(mock.ANY, instance_id)
def test_server_get_failed(self): @ddt.data(
nova.novaclient.side_effect = nova_exception.NotFound(404) {'nova_e': nova_exception.NotFound(404),
'manila_e': exception.InstanceNotFound},
{'nova_e': nova_exception.BadRequest(400),
'manila_e': exception.InvalidInput},
)
@ddt.unpack
def test_server_get_failed(self, nova_e, manila_e):
nova.novaclient.side_effect = nova_e
instance_id = 'instance_id' instance_id = 'instance_id'
self.assertRaises(exception.InstanceNotFound, self.assertRaises(manila_e, self.api.server_get, self.ctx, instance_id)
self.api.server_get, self.ctx, instance_id)
def test_server_list(self): def test_server_list(self):
self.assertEqual([{'id': 'id1'}, {'id': 'id2'}], self.assertEqual([{'id': 'id1'}, {'id': 'id2'}],

View File

@ -13,6 +13,7 @@
# under the License. # under the License.
from cinderclient import exceptions as cinder_exception from cinderclient import exceptions as cinder_exception
import ddt
import mock import mock
from manila import context from manila import context
@ -40,6 +41,7 @@ class FakeCinderClient(object):
self.volume_snapshots = self.volumes self.volume_snapshots = self.volumes
@ddt.ddt
class CinderApiTestCase(test.TestCase): class CinderApiTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(CinderApiTestCase, self).setUp() super(CinderApiTestCase, self).setUp()
@ -59,11 +61,17 @@ class CinderApiTestCase(test.TestCase):
result = self.api.get(self.ctx, volume_id) result = self.api.get(self.ctx, volume_id)
self.assertEqual(result['id'], volume_id) self.assertEqual(result['id'], volume_id)
def test_get_failed(self): @ddt.data(
cinder.cinderclient.side_effect = cinder_exception.NotFound(404) {'cinder_e': cinder_exception.NotFound(404),
'manila_e': exception.VolumeNotFound},
{'cinder_e': cinder_exception.BadRequest(400),
'manila_e': exception.InvalidInput},
)
@ddt.unpack
def test_get_failed(self, cinder_e, manila_e):
cinder.cinderclient.side_effect = cinder_e
volume_id = 'volume_id' volume_id = 'volume_id'
self.assertRaises(exception.VolumeNotFound, self.assertRaises(manila_e, self.api.get, self.ctx, volume_id)
self.api.get, self.ctx, volume_id)
def test_create(self): def test_create(self):
result = self.api.create(self.ctx, 1, '', '') result = self.api.create(self.ctx, 1, '', '')

View File

@ -18,13 +18,13 @@ Handles all requests relating to volumes + cinder.
""" """
import copy import copy
import sys
from cinderclient import exceptions as cinder_exception from cinderclient import exceptions as cinder_exception
from cinderclient import service_catalog from cinderclient import service_catalog
from cinderclient.v2 import client as cinder_client from cinderclient.v2 import client as cinder_client
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log from oslo_log import log
import six
import manila.context as ctxt import manila.context as ctxt
from manila.db import base from manila.db import base
@ -176,13 +176,11 @@ def translate_volume_exception(method):
def wrapper(self, ctx, volume_id, *args, **kwargs): def wrapper(self, ctx, volume_id, *args, **kwargs):
try: try:
res = method(self, ctx, volume_id, *args, **kwargs) res = method(self, ctx, volume_id, *args, **kwargs)
except cinder_exception.ClientException: except cinder_exception.ClientException as e:
exc_type, exc_value, exc_trace = sys.exc_info() if isinstance(e, cinder_exception.NotFound):
if isinstance(exc_value, cinder_exception.NotFound): raise exception.VolumeNotFound(volume_id=volume_id)
exc_value = exception.VolumeNotFound(volume_id=volume_id) elif isinstance(e, cinder_exception.BadRequest):
elif isinstance(exc_value, cinder_exception.BadRequest): raise exception.InvalidInput(reason=six.text_type(e))
exc_value = exception.InvalidInput(reason=exc_value.message)
raise exc_value, None, exc_trace
return res return res
return wrapper return wrapper
@ -195,12 +193,9 @@ def translate_snapshot_exception(method):
def wrapper(self, ctx, snapshot_id, *args, **kwargs): def wrapper(self, ctx, snapshot_id, *args, **kwargs):
try: try:
res = method(self, ctx, snapshot_id, *args, **kwargs) res = method(self, ctx, snapshot_id, *args, **kwargs)
except cinder_exception.ClientException: except cinder_exception.ClientException as e:
exc_type, exc_value, exc_trace = sys.exc_info() if isinstance(e, cinder_exception.NotFound):
if isinstance(exc_value, cinder_exception.NotFound): raise exception.VolumeSnapshotNotFound(snapshot_id=snapshot_id)
exc_value = (
exception.VolumeSnapshotNotFound(snapshot_id=snapshot_id))
raise exc_value, None, exc_trace
return res return res
return wrapper return wrapper
@ -303,14 +298,14 @@ class API(base.Base):
item = cinderclient(context).volumes.create(size, **kwargs) item = cinderclient(context).volumes.create(size, **kwargs)
return _untranslate_volume_summary_view(context, item) return _untranslate_volume_summary_view(context, item)
except cinder_exception.BadRequest as e: except cinder_exception.BadRequest as e:
raise exception.InvalidInput(reason=e.message) raise exception.InvalidInput(reason=six.text_type(e))
except cinder_exception.NotFound: except cinder_exception.NotFound:
raise exception.NotFound( raise exception.NotFound(
_("Error in creating cinder " _("Error in creating cinder "
"volume. Cinder volume type %s not exist. Check parameter " "volume. Cinder volume type %s not exist. Check parameter "
"cinder_volume_type in configuration file.") % volume_type) "cinder_volume_type in configuration file.") % volume_type)
except Exception as e: except Exception as e:
raise exception.ManilaException(e.message) raise exception.ManilaException(e)
@translate_volume_exception @translate_volume_exception
def extend(self, context, volume_id, new_size): def extend(self, context, volume_id, new_size):