feat: ability to redefine ConfigError message
This commit is contained in:
parent
7ecec88881
commit
c24b0642a7
2 changed files with 40 additions and 68 deletions
|
|
@ -22,7 +22,7 @@ from typing import Any, Optional, Union, Callable, Dict, List
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
import weakref
|
import weakref
|
||||||
|
|
||||||
from .error import PropertiesOptionError, ConfigError, LeadershipError, ValueWarning, CancelParam, display_list
|
from .error import PropertiesOptionError, ConfigError, LeadershipError, ValueWarning, CancelParam, display_list, errors
|
||||||
from .i18n import _
|
from .i18n import _
|
||||||
from .setting import undefined, ConfigBag
|
from .setting import undefined, ConfigBag
|
||||||
from .function import FUNCTION_WAITING_FOR_DICT, FUNCTION_WAITING_FOR_ERROR
|
from .function import FUNCTION_WAITING_FOR_DICT, FUNCTION_WAITING_FOR_ERROR
|
||||||
|
|
@ -446,14 +446,7 @@ def manager_callback(
|
||||||
or param.raisepropertyerror
|
or param.raisepropertyerror
|
||||||
):
|
):
|
||||||
raise err from err
|
raise err from err
|
||||||
display_name = subconfig.option.impl_get_display_name(
|
errors.raise_carry_out_calculation_error(subconfig, _("unable to carry out a calculation for {0}, {1}"), err)
|
||||||
subconfig, with_quote=True
|
|
||||||
)
|
|
||||||
raise ConfigError(
|
|
||||||
_("unable to carry out a calculation for {}, {}").format(
|
|
||||||
display_name, err
|
|
||||||
)
|
|
||||||
) from err
|
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
display_name = subconfig.option.impl_get_display_name(
|
display_name = subconfig.option.impl_get_display_name(
|
||||||
subconfig, with_quote=True
|
subconfig, with_quote=True
|
||||||
|
|
@ -471,14 +464,7 @@ def manager_callback(
|
||||||
["configerror"],
|
["configerror"],
|
||||||
config_bag.context.get_settings(),
|
config_bag.context.get_settings(),
|
||||||
)
|
)
|
||||||
display_name = subconfig.option.impl_get_display_name(
|
errors.raise_carry_out_calculation_error(subconfig, _("unable to get value for calculating {0}, {1}"), err)
|
||||||
subconfig, with_quote=True
|
|
||||||
)
|
|
||||||
raise ConfigError(
|
|
||||||
_("unable to get value for calculating {0}, {1}").format(
|
|
||||||
display_name, err
|
|
||||||
)
|
|
||||||
) from err
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def get_option_bag(
|
def get_option_bag(
|
||||||
|
|
@ -510,12 +496,7 @@ def manager_callback(
|
||||||
# raise PropertiesOptionError (which is catched) because must not add value None in carry_out_calculation
|
# raise PropertiesOptionError (which is catched) because must not add value None in carry_out_calculation
|
||||||
if param.notraisepropertyerror or param.raisepropertyerror:
|
if param.notraisepropertyerror or param.raisepropertyerror:
|
||||||
raise err from err
|
raise err from err
|
||||||
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
errors.raise_carry_out_calculation_error(subconfig, _("unable to carry out a calculation for {0}, {1}"), err, option=option)
|
||||||
raise ConfigError(
|
|
||||||
_("unable to carry out a calculation for {}, {}").format(
|
|
||||||
display_name, err
|
|
||||||
)
|
|
||||||
) from err
|
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
|
@ -531,12 +512,7 @@ def manager_callback(
|
||||||
["configerror"],
|
["configerror"],
|
||||||
config_bag.context.get_settings(),
|
config_bag.context.get_settings(),
|
||||||
)
|
)
|
||||||
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
errors.raise_carry_out_calculation_error(subconfig, _("unable to unable to get value for calculating {0}, {1}"), err, option=option)
|
||||||
raise ConfigError(
|
|
||||||
_("unable to get value for calculating {0}, {1}").format(
|
|
||||||
display_name, err
|
|
||||||
)
|
|
||||||
) from err
|
|
||||||
return subsubconfig
|
return subsubconfig
|
||||||
|
|
||||||
if isinstance(param, ParamValue):
|
if isinstance(param, ParamValue):
|
||||||
|
|
@ -553,15 +529,10 @@ def manager_callback(
|
||||||
true_path=subconfig.path,
|
true_path=subconfig.path,
|
||||||
)
|
)
|
||||||
if isinstance(isubconfig, list):
|
if isinstance(isubconfig, list):
|
||||||
display_name = option.impl_get_display_name(
|
|
||||||
subconfig, with_quote=True
|
|
||||||
)
|
|
||||||
search_name = search_option.impl_get_display_name(
|
search_name = search_option.impl_get_display_name(
|
||||||
None, with_quote=True
|
None, with_quote=True
|
||||||
)
|
)
|
||||||
raise ConfigError(
|
errors.raise_carry_out_calculation_error(subconfig, _("cannot find information for {0}, {1} is a dynamic option"), None, option=option, extra_keys=[search_name])
|
||||||
f"cannot find information for {display_name}, {search_name} is a dynamic option"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
isubconfig = get_option_bag(
|
isubconfig = get_option_bag(
|
||||||
config_bag,
|
config_bag,
|
||||||
|
|
@ -580,12 +551,7 @@ def manager_callback(
|
||||||
param.default_value,
|
param.default_value,
|
||||||
)
|
)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
errors.raise_carry_out_calculation_error(subconfig, _("unable to get value for calculating {0}, {1}"), err, option=option)
|
||||||
raise ConfigError(
|
|
||||||
_("unable to get value for calculating {0}, {1}").format(
|
|
||||||
display_name, err
|
|
||||||
)
|
|
||||||
) from err
|
|
||||||
|
|
||||||
if isinstance(param, ParamIndex):
|
if isinstance(param, ParamIndex):
|
||||||
return index
|
return index
|
||||||
|
|
@ -595,14 +561,7 @@ def manager_callback(
|
||||||
not option.impl_is_optiondescription()
|
not option.impl_is_optiondescription()
|
||||||
or not option.impl_is_dynoptiondescription()
|
or not option.impl_is_dynoptiondescription()
|
||||||
):
|
):
|
||||||
display_name = subconfig.option.impl_get_display_name(
|
errors.raise_carry_out_calculation_error(subconfig, _("option {0} is not a dynoptiondescription or in a dynoptiondescription"), None, option=option)
|
||||||
subconfig, with_quote=True
|
|
||||||
)
|
|
||||||
raise ConfigError(
|
|
||||||
_(
|
|
||||||
"option {0} is not a dynoptiondescription or in a dynoptiondescription"
|
|
||||||
).format(display_name)
|
|
||||||
)
|
|
||||||
if subconfig.identifiers is None:
|
if subconfig.identifiers is None:
|
||||||
# if uncalculated
|
# if uncalculated
|
||||||
return
|
return
|
||||||
|
|
@ -685,8 +644,10 @@ def manager_callback(
|
||||||
raise CancelParam(callbk_option.impl_getpath(), child_path)
|
raise CancelParam(callbk_option.impl_getpath(), child_path)
|
||||||
|
|
||||||
identifiers = display_list(doption.get_identifiers(parent), add_quote=True)
|
identifiers = display_list(doption.get_identifiers(parent), add_quote=True)
|
||||||
msg = _('cannot calculate arguments for "{0}", cannot find dynamic variable "{1}" with identifier "{2}", list of valid identifiers: {3}').format(subconfig.path, doption.impl_getpath(), identifier, identifiers)
|
doption_name = doption.impl_get_display_name(
|
||||||
raise ConfigError(msg) from err
|
None, with_quote=True
|
||||||
|
)
|
||||||
|
errors.raise_carry_out_calculation_error(subconfig, _('cannot calculate arguments for "{0}", cannot find dynamic variable "{1}" with identifier "{2}", list of valid identifiers: {3}'), err, extra_keys=[doption_name, identifier, identifiers])
|
||||||
new_parents.append(
|
new_parents.append(
|
||||||
parent.get_child(
|
parent.get_child(
|
||||||
doption,
|
doption,
|
||||||
|
|
@ -790,9 +751,7 @@ def carry_out_calculation(
|
||||||
and option.impl_is_follower()
|
and option.impl_is_follower()
|
||||||
and index is None
|
and index is None
|
||||||
):
|
):
|
||||||
raise ConfigError(
|
errors.raise_carry_out_calculation_error(subconfig, _("the follower {0} must have index in carry_out_calculation!"), None, option=option)
|
||||||
f"the follower {option.impl_get_display_name(subconfig, with_quote=True)} must have index in carry_out_calculation!"
|
|
||||||
)
|
|
||||||
|
|
||||||
def fake_items(iterator):
|
def fake_items(iterator):
|
||||||
return ((None, i) for i in iterator)
|
return ((None, i) for i in iterator)
|
||||||
|
|
@ -921,19 +880,14 @@ def calculate(
|
||||||
error = err
|
error = err
|
||||||
if args or kwargs:
|
if args or kwargs:
|
||||||
msg = _(
|
msg = _(
|
||||||
'unexpected error "{0}" in function "{1}" with arguments "{3}" and "{4}" '
|
'unexpected error "{1}" in function "{2}" with arguments "{3}" and "{4}" '
|
||||||
"for option {2}"
|
"for option {0}"
|
||||||
).format(
|
|
||||||
str(error),
|
|
||||||
callback.__name__,
|
|
||||||
subconfig.option.impl_get_display_name(subconfig, with_quote=True),
|
|
||||||
args,
|
|
||||||
kwargs,
|
|
||||||
)
|
)
|
||||||
|
extra_keys = [callback.__name__,
|
||||||
|
args,
|
||||||
|
kwargs,
|
||||||
|
]
|
||||||
else:
|
else:
|
||||||
msg = _('unexpected error "{0}" in function "{1}" for option {2}' "").format(
|
msg = _('unexpected error "{1}" in function "{2}" for option {0}')
|
||||||
str(error),
|
extra_keys = [callback.__name__]
|
||||||
callback.__name__,
|
errors.raise_carry_out_calculation_error(subconfig, msg, error, extra_keys=extra_keys)
|
||||||
subconfig.option.impl_get_display_name(subconfig, with_quote=True),
|
|
||||||
)
|
|
||||||
raise ConfigError(msg) from error
|
|
||||||
|
|
|
||||||
|
|
@ -254,3 +254,21 @@ class CancelParam(Exception):
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class Errors:
|
||||||
|
@staticmethod
|
||||||
|
def raise_carry_out_calculation_error(subconfig, message, original_error, option=None, extra_keys=[]):
|
||||||
|
if option is None:
|
||||||
|
option = subconfig.option
|
||||||
|
display_name = option.impl_get_display_name(
|
||||||
|
subconfig, with_quote=True
|
||||||
|
)
|
||||||
|
if original_error:
|
||||||
|
raise ConfigError(
|
||||||
|
message.format(display_name, original_error, *extra_keys)
|
||||||
|
) from original_error
|
||||||
|
raise ConfigError(message.format(display_name, extra_keys))
|
||||||
|
|
||||||
|
|
||||||
|
errors = Errors()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue