1
0
mirror of https://github.com/github/octodns.git synced 2024-05-11 05:55:00 +00:00

POC of config/validation errors with context. Implemented by YAML for all it's cases

This commit is contained in:
Ross McFarland
2023-07-28 17:14:01 -07:00
parent 7b2a1d4429
commit 1776d558b5
5 changed files with 97 additions and 37 deletions

View File

@@ -46,14 +46,21 @@ class Record(EqualityTupleMixin):
reasons.append('invalid record, whitespace is not allowed')
fqdn = f'{name}.{zone.name}' if name else zone.name
context = getattr(data, 'context', None)
try:
_type = data['type']
except KeyError:
raise Exception(f'Invalid record {idna_decode(fqdn)}, missing type')
msg = f'Invalid record {idna_decode(fqdn)}, missing type'
if context:
msg += f', {context}'
raise Exception(msg)
try:
_class = cls._CLASSES[_type]
except KeyError:
raise Exception(f'Unknown record type: "{_type}"')
msg = f'Unknown record type: "{_type}"'
if context:
msg += f', {context}'
raise Exception(msg)
reasons.extend(_class.validate(name, fqdn, data))
try:
lenient |= data['octodns']['lenient']
@@ -61,9 +68,11 @@ class Record(EqualityTupleMixin):
pass
if reasons:
if lenient:
cls.log.warning(ValidationError.build_message(fqdn, reasons))
cls.log.warning(
ValidationError.build_message(fqdn, reasons, context)
)
else:
raise ValidationError(fqdn, reasons)
raise ValidationError(fqdn, reasons, context)
return _class(zone, name, data, source=source)
@classmethod

View File

@@ -11,11 +11,16 @@ class RecordException(Exception):
class ValidationError(RecordException):
@classmethod
def build_message(cls, fqdn, reasons):
def build_message(cls, fqdn, reasons, context=None):
reasons = '\n - '.join(reasons)
return f'Invalid record "{idna_decode(fqdn)}"\n - {reasons}'
msg = f'Invalid record "{idna_decode(fqdn)}"'
if context:
msg += f', {context}'
msg += f'\n - {reasons}'
return msg
def __init__(self, fqdn, reasons):
super().__init__(self.build_message(fqdn, reasons))
def __init__(self, fqdn, reasons, context=None):
super().__init__(self.build_message(fqdn, reasons, context))
self.fqdn = fqdn
self.reasons = reasons
self.context = context