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
|
||||
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 .setting import undefined, ConfigBag
|
||||
from .function import FUNCTION_WAITING_FOR_DICT, FUNCTION_WAITING_FOR_ERROR
|
||||
|
|
@ -446,14 +446,7 @@ def manager_callback(
|
|||
or param.raisepropertyerror
|
||||
):
|
||||
raise err from err
|
||||
display_name = subconfig.option.impl_get_display_name(
|
||||
subconfig, with_quote=True
|
||||
)
|
||||
raise ConfigError(
|
||||
_("unable to carry out a calculation for {}, {}").format(
|
||||
display_name, err
|
||||
)
|
||||
) from err
|
||||
errors.raise_carry_out_calculation_error(subconfig, _("unable to carry out a calculation for {0}, {1}"), err)
|
||||
except ValueError as err:
|
||||
display_name = subconfig.option.impl_get_display_name(
|
||||
subconfig, with_quote=True
|
||||
|
|
@ -471,14 +464,7 @@ def manager_callback(
|
|||
["configerror"],
|
||||
config_bag.context.get_settings(),
|
||||
)
|
||||
display_name = subconfig.option.impl_get_display_name(
|
||||
subconfig, with_quote=True
|
||||
)
|
||||
raise ConfigError(
|
||||
_("unable to get value for calculating {0}, {1}").format(
|
||||
display_name, err
|
||||
)
|
||||
) from err
|
||||
errors.raise_carry_out_calculation_error(subconfig, _("unable to get value for calculating {0}, {1}"), err)
|
||||
return value
|
||||
|
||||
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
|
||||
if param.notraisepropertyerror or param.raisepropertyerror:
|
||||
raise err from err
|
||||
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
||||
raise ConfigError(
|
||||
_("unable to carry out a calculation for {}, {}").format(
|
||||
display_name, err
|
||||
)
|
||||
) from err
|
||||
errors.raise_carry_out_calculation_error(subconfig, _("unable to carry out a calculation for {0}, {1}"), err, option=option)
|
||||
except ValueError as err:
|
||||
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
||||
raise ValueError(
|
||||
|
|
@ -531,12 +512,7 @@ def manager_callback(
|
|||
["configerror"],
|
||||
config_bag.context.get_settings(),
|
||||
)
|
||||
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
||||
raise ConfigError(
|
||||
_("unable to get value for calculating {0}, {1}").format(
|
||||
display_name, err
|
||||
)
|
||||
) from err
|
||||
errors.raise_carry_out_calculation_error(subconfig, _("unable to unable to get value for calculating {0}, {1}"), err, option=option)
|
||||
return subsubconfig
|
||||
|
||||
if isinstance(param, ParamValue):
|
||||
|
|
@ -553,15 +529,10 @@ def manager_callback(
|
|||
true_path=subconfig.path,
|
||||
)
|
||||
if isinstance(isubconfig, list):
|
||||
display_name = option.impl_get_display_name(
|
||||
subconfig, with_quote=True
|
||||
)
|
||||
search_name = search_option.impl_get_display_name(
|
||||
None, with_quote=True
|
||||
)
|
||||
raise ConfigError(
|
||||
f"cannot find information for {display_name}, {search_name} is a dynamic option"
|
||||
)
|
||||
errors.raise_carry_out_calculation_error(subconfig, _("cannot find information for {0}, {1} is a dynamic option"), None, option=option, extra_keys=[search_name])
|
||||
else:
|
||||
isubconfig = get_option_bag(
|
||||
config_bag,
|
||||
|
|
@ -580,12 +551,7 @@ def manager_callback(
|
|||
param.default_value,
|
||||
)
|
||||
except ValueError as err:
|
||||
display_name = option.impl_get_display_name(subconfig, with_quote=True)
|
||||
raise ConfigError(
|
||||
_("unable to get value for calculating {0}, {1}").format(
|
||||
display_name, err
|
||||
)
|
||||
) from err
|
||||
errors.raise_carry_out_calculation_error(subconfig, _("unable to get value for calculating {0}, {1}"), err, option=option)
|
||||
|
||||
if isinstance(param, ParamIndex):
|
||||
return index
|
||||
|
|
@ -595,14 +561,7 @@ def manager_callback(
|
|||
not option.impl_is_optiondescription()
|
||||
or not option.impl_is_dynoptiondescription()
|
||||
):
|
||||
display_name = subconfig.option.impl_get_display_name(
|
||||
subconfig, with_quote=True
|
||||
)
|
||||
raise ConfigError(
|
||||
_(
|
||||
"option {0} is not a dynoptiondescription or in a dynoptiondescription"
|
||||
).format(display_name)
|
||||
)
|
||||
errors.raise_carry_out_calculation_error(subconfig, _("option {0} is not a dynoptiondescription or in a dynoptiondescription"), None, option=option)
|
||||
if subconfig.identifiers is None:
|
||||
# if uncalculated
|
||||
return
|
||||
|
|
@ -685,8 +644,10 @@ def manager_callback(
|
|||
raise CancelParam(callbk_option.impl_getpath(), child_path)
|
||||
|
||||
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)
|
||||
raise ConfigError(msg) from err
|
||||
doption_name = doption.impl_get_display_name(
|
||||
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(
|
||||
parent.get_child(
|
||||
doption,
|
||||
|
|
@ -790,9 +751,7 @@ def carry_out_calculation(
|
|||
and option.impl_is_follower()
|
||||
and index is None
|
||||
):
|
||||
raise ConfigError(
|
||||
f"the follower {option.impl_get_display_name(subconfig, with_quote=True)} must have index in carry_out_calculation!"
|
||||
)
|
||||
errors.raise_carry_out_calculation_error(subconfig, _("the follower {0} must have index in carry_out_calculation!"), None, option=option)
|
||||
|
||||
def fake_items(iterator):
|
||||
return ((None, i) for i in iterator)
|
||||
|
|
@ -921,19 +880,14 @@ def calculate(
|
|||
error = err
|
||||
if args or kwargs:
|
||||
msg = _(
|
||||
'unexpected error "{0}" in function "{1}" with arguments "{3}" and "{4}" '
|
||||
"for option {2}"
|
||||
).format(
|
||||
str(error),
|
||||
callback.__name__,
|
||||
subconfig.option.impl_get_display_name(subconfig, with_quote=True),
|
||||
args,
|
||||
kwargs,
|
||||
'unexpected error "{1}" in function "{2}" with arguments "{3}" and "{4}" '
|
||||
"for option {0}"
|
||||
)
|
||||
extra_keys = [callback.__name__,
|
||||
args,
|
||||
kwargs,
|
||||
]
|
||||
else:
|
||||
msg = _('unexpected error "{0}" in function "{1}" for option {2}' "").format(
|
||||
str(error),
|
||||
callback.__name__,
|
||||
subconfig.option.impl_get_display_name(subconfig, with_quote=True),
|
||||
)
|
||||
raise ConfigError(msg) from error
|
||||
msg = _('unexpected error "{1}" in function "{2}" for option {0}')
|
||||
extra_keys = [callback.__name__]
|
||||
errors.raise_carry_out_calculation_error(subconfig, msg, error, extra_keys=extra_keys)
|
||||
|
|
|
|||
|
|
@ -254,3 +254,21 @@ class CancelParam(Exception):
|
|||
|
||||
def __bool__(self):
|
||||
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