Improve StackValidationFailed exception

This patch adds to StackValidationFailed new
parameters 'error' for specified error like
'Property error' and 'path' for path in template
where validation failed. StackValidationFailed
message format is 'error : path: message', where
path can looks like
'resource_name.section_name.section_item_name'.

Change-Id: I3673b3220fb613c6ec28318f65ed4adcacf6ec7c
This commit is contained in:
Peter Razumovsky 2015-03-03 14:23:38 +03:00
parent dd780d8b8b
commit f85f6359f4
2 changed files with 123 additions and 1 deletions

View File

@ -269,7 +269,41 @@ class StackExists(HeatException):
class StackValidationFailed(HeatException):
msg_fmt = _("%(message)s")
msg_fmt = _("%(error)s%(path)s%(message)s")
def __init__(self, error=None, path=None, message=None):
self.error = error or ''
self.path = []
if path is not None:
if isinstance(path, list):
self.path = path
elif isinstance(path, six.string_types):
self.path = [path]
result_path = ''
for path_item in self.path:
if isinstance(path_item, int) or path_item.isdigit():
result_path += '[%s]' % path_item
elif len(result_path) > 0:
result_path += '.%s' % path_item
else:
result_path = path_item
self.error_message = message or ''
super(StackValidationFailed, self).__init__(
error=('%s : ' % self.error if self.error != '' else ''),
path=('%s: ' % result_path if len(result_path) > 0 else ''),
message=self.error_message
)
def error(self):
return self.error
def path(self):
return self.path
def error_message(self):
return self.error_message
class InvalidSchemaError(HeatException):

View File

@ -39,3 +39,91 @@ class TestHeatException(common.HeatTestCase):
message = "This format %(message)s should work"
err = exception.Error(message)
self.assertEqual(message, six.text_type(err))
class TestStackValidationFailed(common.HeatTestCase):
scenarios = [
('test_full_exception', dict(
kwargs=dict(
error='Error',
path=['some', 'path'],
message='Some message'),
expected='Error : some.path: Some message',
called_error='Error',
called_path=['some', 'path'],
called_msg='Some message'
)),
('test_no_error_exception', dict(
kwargs=dict(
path=['some', 'path'],
message='Chain letter'),
expected='some.path: Chain letter',
called_error='',
called_path=['some', 'path'],
called_msg='Chain letter'
)),
('test_no_path_exception', dict(
kwargs=dict(
error='Error',
message='Just no.'),
expected='Error : Just no.',
called_error='Error',
called_path=[],
called_msg='Just no.'
)),
('test_no_msg_exception', dict(
kwargs=dict(
error='Error',
path=['we', 'lost', 'our', 'message']),
expected='Error : we.lost.our.message: ',
called_error='Error',
called_path=['we', 'lost', 'our', 'message'],
called_msg=''
)),
('test_old_format_exception', dict(
kwargs=dict(
message='Wow. I think I am old error message format.'
),
expected='Wow. I think I am old error message format.',
called_error='',
called_path=[],
called_msg='Wow. I think I am old error message format.'
)),
('test_int_path_item_exception', dict(
kwargs=dict(
path=['null', 0]
),
expected='null[0]: ',
called_error='',
called_path=['null', 0],
called_msg=''
)),
('test_digit_path_item_exception', dict(
kwargs=dict(
path=['null', '0']
),
expected='null[0]: ',
called_error='',
called_path=['null', '0'],
called_msg=''
)),
('test_string_path_exception', dict(
kwargs=dict(
path='null[0].not_null'
),
expected='null[0].not_null: ',
called_error='',
called_path=['null[0].not_null'],
called_msg=''
))
]
def test_exception(self):
try:
raise exception.StackValidationFailed(**self.kwargs)
except exception.StackValidationFailed as ex:
self.assertEqual(self.expected, six.text_type(ex))
self.assertEqual(self.called_error, ex.error)
self.assertEqual(self.called_path, ex.path)
self.assertEqual(self.called_msg, ex.error_message)