Skip to content

Commit

Permalink
Add field_name to ConfigurationError messages on init (#2133)
Browse files Browse the repository at this point in the history
* Add field_name to ConfigurationError messages

* Format with black

---------

Co-authored-by: Riccardo Magliocchetti <[email protected]>
  • Loading branch information
radupotop and xrmx authored Dec 24, 2024
1 parent 094652a commit 778797b
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions elasticapm/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -288,12 +295,19 @@ 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


Expand All @@ -315,7 +329,7 @@ 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:
Expand All @@ -337,8 +351,10 @@ 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,
)
Expand All @@ -349,11 +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(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


Expand Down Expand Up @@ -384,7 +400,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

Expand Down

0 comments on commit 778797b

Please sign in to comment.