From d2eca678fa89d2b0973d6f842428f332b3b91ef5 Mon Sep 17 00:00:00 2001 From: Radu Potop Date: Thu, 3 Oct 2024 16:16:28 +0100 Subject: [PATCH 1/2] Add field_name to ConfigurationError messages --- elasticapm/conf/__init__.py | 53 ++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/elasticapm/conf/__init__.py b/elasticapm/conf/__init__.py index 5318d64b5..b10a74837 100644 --- a/elasticapm/conf/__init__.py +++ b/elasticapm/conf/__init__.py @@ -275,7 +275,14 @@ def __call__(self, value, field_name): match = re.match(self.regex, value) if match: return value - raise ConfigurationError("{} does not match pattern {}".format(value, self.verbose_pattern), field_name) + raise ConfigurationError( + "{}={} does not match pattern {}".format( + field_name, + value, + self.verbose_pattern, + ), + field_name, + ) class UnitValidator(object): @@ -288,12 +295,21 @@ def __call__(self, value, field_name): value = str(value) match = re.match(self.regex, value, re.IGNORECASE) if not match: - raise ConfigurationError("{} does not match pattern {}".format(value, self.verbose_pattern), field_name) + raise ConfigurationError( + "{}={} does not match pattern {}".format( + field_name, + value, + self.verbose_pattern, + ), + field_name, + ) val, unit = match.groups() try: val = int(val) * self.unit_multipliers[unit] except KeyError: - raise ConfigurationError("{} is not a supported unit".format(unit), field_name) + raise ConfigurationError( + "{}={} is not a supported unit".format(field_name, unit), field_name + ) return val @@ -315,7 +331,9 @@ def __call__(self, value, field_name): try: value = float(value) except ValueError: - raise ConfigurationError("{} is not a float".format(value), field_name) + raise ConfigurationError( + "{}={} is not a float".format(field_name, value), field_name + ) multiplier = 10**self.precision rounded = math.floor(value * multiplier + 0.5) / multiplier if rounded == 0 and self.minimum and value != 0: @@ -337,8 +355,12 @@ def __init__(self, range_start, range_end, range_desc) -> None: def __call__(self, value, field_name): if self.range_start <= value <= self.range_end: raise ConfigurationError( - "{} cannot be in range: {}".format( - value, self.range_desc.format(**{"range_start": self.range_start, "range_end": self.range_end}) + "{}={} cannot be in range: {}".format( + field_name, + value, + self.range_desc.format( + **{"range_start": self.range_start, "range_end": self.range_end} + ), ), field_name, ) @@ -349,11 +371,17 @@ class FileIsReadableValidator(object): def __call__(self, value, field_name): value = os.path.normpath(value) if not os.path.exists(value): - raise ConfigurationError("{} does not exist".format(value), field_name) + raise ConfigurationError( + "{}={} does not exist".format(field_name, value), field_name + ) elif not os.path.isfile(value): - raise ConfigurationError("{} is not a file".format(value), field_name) + raise ConfigurationError( + "{}={} is not a file".format(field_name, value), field_name + ) elif not os.access(value, os.R_OK): - raise ConfigurationError("{} is not readable".format(value), field_name) + raise ConfigurationError( + "{}={} is not readable".format(field_name, value), field_name + ) return value @@ -384,7 +412,12 @@ def __call__(self, value, field_name): ret = self.valid_values.get(value.lower()) if ret is None: raise ConfigurationError( - "{} is not in the list of valid values: {}".format(value, list(self.valid_values.values())), field_name + "{}={} is not in the list of valid values: {}".format( + field_name, + value, + list(self.valid_values.values()), + ), + field_name, ) return ret From 1eac69d6485fe2979a3b022c4973ece638b92858 Mon Sep 17 00:00:00 2001 From: Radu Potop Date: Thu, 10 Oct 2024 09:20:26 +0100 Subject: [PATCH 2/2] Format with black --- elasticapm/conf/__init__.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/elasticapm/conf/__init__.py b/elasticapm/conf/__init__.py index b10a74837..5b851c9a5 100644 --- a/elasticapm/conf/__init__.py +++ b/elasticapm/conf/__init__.py @@ -307,9 +307,7 @@ def __call__(self, value, field_name): try: val = int(val) * self.unit_multipliers[unit] except KeyError: - raise ConfigurationError( - "{}={} is not a supported unit".format(field_name, unit), field_name - ) + raise ConfigurationError("{}={} is not a supported unit".format(field_name, unit), field_name) return val @@ -331,9 +329,7 @@ def __call__(self, value, field_name): try: value = float(value) except ValueError: - raise ConfigurationError( - "{}={} is not a float".format(field_name, value), field_name - ) + raise ConfigurationError("{}={} is not a float".format(field_name, value), field_name) multiplier = 10**self.precision rounded = math.floor(value * multiplier + 0.5) / multiplier if rounded == 0 and self.minimum and value != 0: @@ -358,9 +354,7 @@ def __call__(self, value, field_name): "{}={} cannot be in range: {}".format( field_name, value, - self.range_desc.format( - **{"range_start": self.range_start, "range_end": self.range_end} - ), + self.range_desc.format(**{"range_start": self.range_start, "range_end": self.range_end}), ), field_name, ) @@ -371,17 +365,11 @@ class FileIsReadableValidator(object): def __call__(self, value, field_name): value = os.path.normpath(value) if not os.path.exists(value): - raise ConfigurationError( - "{}={} does not exist".format(field_name, value), field_name - ) + raise ConfigurationError("{}={} does not exist".format(field_name, value), field_name) elif not os.path.isfile(value): - raise ConfigurationError( - "{}={} is not a file".format(field_name, value), field_name - ) + raise ConfigurationError("{}={} is not a file".format(field_name, value), field_name) elif not os.access(value, os.R_OK): - raise ConfigurationError( - "{}={} is not readable".format(field_name, value), field_name - ) + raise ConfigurationError("{}={} is not readable".format(field_name, value), field_name) return value