From c526255aeda03adabbd4952814f21439a1d835bc Mon Sep 17 00:00:00 2001 From: Gorka Eguileor Date: Fri, 6 May 2016 16:11:00 +0200 Subject: [PATCH] Fix uncaught NotFound exceptions There are a couple of places in the code where we were not catching NotFound exceptions which resulted in getting the exception logged incorrectly in the logs and the error message being returned by the WSGI layer being slightly off. Cases where this was happening are volume encryption metadata and volume transfer. From the user perspective the error was very similar to what it was expected, but it was preceeded by the name of the Cinder exception (ex: VolumeNotFound: ). This patch catches all NotFound exception at the WSGI level and properly converts them, that way we prevent the exception to propagate to the middleware fault level and generate a log exception and return slightly different error. Change-Id: I8971afedd68854d2840ab551b420ca083e8ee0d6 --- cinder/api/openstack/wsgi.py | 2 +- .../unit/api/contrib/test_volume_encryption_metadata.py | 6 ++---- cinder/tests/unit/api/contrib/test_volume_transfer.py | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cinder/api/openstack/wsgi.py b/cinder/api/openstack/wsgi.py index 6314cee863b..459347e7ed9 100644 --- a/cinder/api/openstack/wsgi.py +++ b/cinder/api/openstack/wsgi.py @@ -595,7 +595,7 @@ class ResourceExceptionHandler(object): raise Fault(webob.exc.HTTPForbidden(explanation=msg)) elif isinstance(ex_value, exception.VersionNotFoundForAPIMethod): raise - elif isinstance(ex_value, exception.Invalid): + elif isinstance(ex_value, (exception.Invalid, exception.NotFound)): raise Fault(exception.ConvertedException( code=ex_value.code, explanation=six.text_type(ex_value))) elif isinstance(ex_value, TypeError): diff --git a/cinder/tests/unit/api/contrib/test_volume_encryption_metadata.py b/cinder/tests/unit/api/contrib/test_volume_encryption_metadata.py index d7f338801a7..1053eee2c44 100644 --- a/cinder/tests/unit/api/contrib/test_volume_encryption_metadata.py +++ b/cinder/tests/unit/api/contrib/test_volume_encryption_metadata.py @@ -112,8 +112,7 @@ class VolumeEncryptionMetadataTest(test.TestCase): res_dict = jsonutils.loads(res.body) expected = {'itemNotFound': {'code': 404, - 'message': 'VolumeNotFound: Volume ' - '%s could not be found.' + 'message': 'Volume %s could not be found.' % bad_volume_id}} self.assertEqual(expected, res_dict) @@ -168,8 +167,7 @@ class VolumeEncryptionMetadataTest(test.TestCase): res_dict = jsonutils.loads(res.body) expected = {'itemNotFound': {'code': 404, - 'message': 'VolumeNotFound: Volume ' - '%s could not be found.' + 'message': 'Volume %s could not be found.' % bad_volume_id}} self.assertEqual(expected, res_dict) diff --git a/cinder/tests/unit/api/contrib/test_volume_transfer.py b/cinder/tests/unit/api/contrib/test_volume_transfer.py index 20859242f17..fac3a956b96 100644 --- a/cinder/tests/unit/api/contrib/test_volume_transfer.py +++ b/cinder/tests/unit/api/contrib/test_volume_transfer.py @@ -430,7 +430,7 @@ class VolumeTransferAPITestCase(test.TestCase): self.assertEqual(404, res.status_int) self.assertEqual(404, res_dict['itemNotFound']['code']) - self.assertEqual('TransferNotFound: Transfer %s could not be found.' % + self.assertEqual('Transfer %s could not be found.' % fake.WILL_NOT_BE_FOUND_ID, res_dict['itemNotFound']['message'])