Skip to content

Commit 778797b

Browse files
radupotopxrmx
andauthored
Add field_name to ConfigurationError messages on init (#2133)
* Add field_name to ConfigurationError messages * Format with black --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent 094652a commit 778797b

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

elasticapm/conf/__init__.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,14 @@ def __call__(self, value, field_name):
275275
match = re.match(self.regex, value)
276276
if match:
277277
return value
278-
raise ConfigurationError("{} does not match pattern {}".format(value, self.verbose_pattern), field_name)
278+
raise ConfigurationError(
279+
"{}={} does not match pattern {}".format(
280+
field_name,
281+
value,
282+
self.verbose_pattern,
283+
),
284+
field_name,
285+
)
279286

280287

281288
class UnitValidator(object):
@@ -288,12 +295,19 @@ def __call__(self, value, field_name):
288295
value = str(value)
289296
match = re.match(self.regex, value, re.IGNORECASE)
290297
if not match:
291-
raise ConfigurationError("{} does not match pattern {}".format(value, self.verbose_pattern), field_name)
298+
raise ConfigurationError(
299+
"{}={} does not match pattern {}".format(
300+
field_name,
301+
value,
302+
self.verbose_pattern,
303+
),
304+
field_name,
305+
)
292306
val, unit = match.groups()
293307
try:
294308
val = int(val) * self.unit_multipliers[unit]
295309
except KeyError:
296-
raise ConfigurationError("{} is not a supported unit".format(unit), field_name)
310+
raise ConfigurationError("{}={} is not a supported unit".format(field_name, unit), field_name)
297311
return val
298312

299313

@@ -315,7 +329,7 @@ def __call__(self, value, field_name):
315329
try:
316330
value = float(value)
317331
except ValueError:
318-
raise ConfigurationError("{} is not a float".format(value), field_name)
332+
raise ConfigurationError("{}={} is not a float".format(field_name, value), field_name)
319333
multiplier = 10**self.precision
320334
rounded = math.floor(value * multiplier + 0.5) / multiplier
321335
if rounded == 0 and self.minimum and value != 0:
@@ -337,8 +351,10 @@ def __init__(self, range_start, range_end, range_desc) -> None:
337351
def __call__(self, value, field_name):
338352
if self.range_start <= value <= self.range_end:
339353
raise ConfigurationError(
340-
"{} cannot be in range: {}".format(
341-
value, self.range_desc.format(**{"range_start": self.range_start, "range_end": self.range_end})
354+
"{}={} cannot be in range: {}".format(
355+
field_name,
356+
value,
357+
self.range_desc.format(**{"range_start": self.range_start, "range_end": self.range_end}),
342358
),
343359
field_name,
344360
)
@@ -349,11 +365,11 @@ class FileIsReadableValidator(object):
349365
def __call__(self, value, field_name):
350366
value = os.path.normpath(value)
351367
if not os.path.exists(value):
352-
raise ConfigurationError("{} does not exist".format(value), field_name)
368+
raise ConfigurationError("{}={} does not exist".format(field_name, value), field_name)
353369
elif not os.path.isfile(value):
354-
raise ConfigurationError("{} is not a file".format(value), field_name)
370+
raise ConfigurationError("{}={} is not a file".format(field_name, value), field_name)
355371
elif not os.access(value, os.R_OK):
356-
raise ConfigurationError("{} is not readable".format(value), field_name)
372+
raise ConfigurationError("{}={} is not readable".format(field_name, value), field_name)
357373
return value
358374

359375

@@ -384,7 +400,12 @@ def __call__(self, value, field_name):
384400
ret = self.valid_values.get(value.lower())
385401
if ret is None:
386402
raise ConfigurationError(
387-
"{} is not in the list of valid values: {}".format(value, list(self.valid_values.values())), field_name
403+
"{}={} is not in the list of valid values: {}".format(
404+
field_name,
405+
value,
406+
list(self.valid_values.values()),
407+
),
408+
field_name,
388409
)
389410
return ret
390411

0 commit comments

Comments
 (0)