error in external function should returns explicit error message

This commit is contained in:
2017-02-11 18:11:05 +01:00
parent 695de030ef
commit 80f6f4ba03
6 changed files with 329 additions and 260 deletions

View File

@ -25,7 +25,7 @@ from .setting import undefined
def carry_out_calculation(option, context, callback, callback_params,
index=undefined, validate=True):
index=undefined, validate=True, is_validator=False):
"""a function that carries out a calculation for an option's value
:param option: the option
@ -38,6 +38,7 @@ def carry_out_calculation(option, context, callback, callback_params,
:type callback_params: dict
:param index: if an option is multi, only calculates the nth value
:type index: int
:param is_validator: to know if carry_out_calculation is used to validate a value
The callback_params is a dict. Key is used to build args (if key is '')
and kwargs (otherwise). Values are tuple of:
@ -216,7 +217,7 @@ def carry_out_calculation(option, context, callback, callback_params,
args.append(val)
else:
kwargs[key] = val
return calculate(callback, args, kwargs)
return calculate(option, callback, is_validator, args, kwargs)
else:
# no value is multi
# return a single value
@ -229,7 +230,7 @@ def carry_out_calculation(option, context, callback, callback_params,
args.append(couple[0])
else:
kwargs[key] = couple[0]
ret = calculate(callback, args, kwargs)
ret = calculate(option, callback, is_validator, args, kwargs)
if not option.impl_is_optiondescription() and callback_params != {} and isinstance(ret, list) and \
option.impl_is_master_slaves('slave'):
if not has_option and index not in [None, undefined]:
@ -243,7 +244,7 @@ def carry_out_calculation(option, context, callback, callback_params,
return ret
def calculate(callback, args, kwargs):
def calculate(option, callback, is_validator, args, kwargs):
"""wrapper that launches the 'callback'
:param callback: callback function
@ -254,4 +255,11 @@ def calculate(callback, args, kwargs):
try:
return callback(*args, **kwargs)
except ValueError as err:
return err
if is_validator:
return err
error = err
except Exception as err:
error = err
raise error.__class__(_('function "{0}" returns "{1}" for option "{2}"').format(callback.func_name,
option.impl_get_display_name(),
str(err)))

View File

@ -571,7 +571,8 @@ class Option(OnlyOption):
value = carry_out_calculation(current_opt, context=context,
callback=validator,
callback_params=validator_params_,
index=_index)
index=_index,
is_validator=True)
if isinstance(value, Exception):
return value