Add error handling to property value resolution
Fixes bug 1224746 Change-Id: I990de0159e0fb49e52afdaab546f8d74372422bf
This commit is contained in:
parent
799b2ff31d
commit
9d4af61655
@ -683,10 +683,12 @@ class Properties(collections.Mapping):
|
||||
prop = self.props[key]
|
||||
|
||||
if key in self.data:
|
||||
value = self.resolve(self.data[key])
|
||||
try:
|
||||
value = self.resolve(self.data[key])
|
||||
return prop.validate_data(value)
|
||||
except ValueError as e:
|
||||
# the resolver function could raise any number of exceptions,
|
||||
# so handle this generically
|
||||
except Exception as e:
|
||||
raise ValueError(self.error_prefix + '%s %s' % (key, str(e)))
|
||||
elif prop.has_default():
|
||||
return prop.default()
|
||||
|
@ -952,7 +952,7 @@ class PropertyTest(testtools.TestCase):
|
||||
def test_list_schema_int_bad_data(self):
|
||||
list_schema = {'Type': 'Integer'}
|
||||
p = properties.Property({'Type': 'List', 'Schema': list_schema})
|
||||
self.assertRaises(TypeError, p.validate_data, [42, 'fish'])
|
||||
self.assertRaises(ValueError, p.validate_data, [42, 'fish'])
|
||||
|
||||
|
||||
class PropertiesTest(testtools.TestCase):
|
||||
@ -986,7 +986,7 @@ class PropertiesTest(testtools.TestCase):
|
||||
self.assertRaises(ValueError, self.props.get, 'required_int')
|
||||
|
||||
def test_integer_bad(self):
|
||||
self.assertRaises(TypeError, self.props.get, 'bad_int')
|
||||
self.assertRaises(ValueError, self.props.get, 'bad_int')
|
||||
|
||||
def test_missing(self):
|
||||
self.assertEqual(self.props['missing'], None)
|
||||
@ -1076,6 +1076,16 @@ class PropertiesTest(testtools.TestCase):
|
||||
props = properties.Properties(schema, {'foo': None})
|
||||
self.assertEqual(['one', 'two'], props['foo'])
|
||||
|
||||
def test_bad_resolver(self):
|
||||
schema = {'foo': {'Type': 'String', 'Default': 'bar'}}
|
||||
|
||||
def bad_resolver(prop):
|
||||
raise Exception('resolution failed!')
|
||||
|
||||
props = properties.Properties(schema, {'foo': 'baz'}, bad_resolver)
|
||||
err = self.assertRaises(ValueError, props.get, 'foo')
|
||||
self.assertEqual('foo resolution failed!', str(err))
|
||||
|
||||
def test_schema_from_params(self):
|
||||
params_snippet = {
|
||||
"DBUsername": {
|
||||
|
Loading…
Reference in New Issue
Block a user