mirror of
https://github.com/samba-team/samba.git
synced 2025-01-22 22:04:08 +03:00
netcmd: models: add FieldError subclass which stores the field
This is so that errors on the CLI show the field name Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
parent
950a70a190
commit
83d321e764
@ -24,6 +24,18 @@ class ModelError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class FieldError(ModelError):
|
||||
"""A ModelError on a specific field."""
|
||||
|
||||
def __init__(self, *args, field=None):
|
||||
self.field = field
|
||||
super().__init__(*args)
|
||||
|
||||
def __str__(self):
|
||||
message = super().__str__()
|
||||
return f"{self.field.name}: {message}"
|
||||
|
||||
|
||||
class MultipleObjectsReturned(ModelError):
|
||||
pass
|
||||
|
||||
|
@ -27,7 +27,7 @@ from ldb import ERR_NO_SUCH_OBJECT, FLAG_MOD_ADD, FLAG_MOD_REPLACE, LdbError,\
|
||||
Message, MessageElement, SCOPE_BASE, SCOPE_SUBTREE, binary_encode
|
||||
from samba.sd_utils import SDUtils
|
||||
|
||||
from .exceptions import DeleteError, DoesNotExist, ModelError,\
|
||||
from .exceptions import DeleteError, DoesNotExist, FieldError,\
|
||||
MultipleObjectsReturned, ProtectError, UnprotectError
|
||||
from .fields import DateTimeField, DnField, Field, GUIDField, IntegerField,\
|
||||
StringField
|
||||
@ -362,7 +362,7 @@ class Model(metaclass=ModelMeta):
|
||||
try:
|
||||
db_value = field.to_db_value(ldb, value, FLAG_MOD_ADD)
|
||||
except ValueError as e:
|
||||
raise ModelError(e)
|
||||
raise FieldError(e, field=field)
|
||||
|
||||
# Don't add empty fields.
|
||||
if db_value is not None and len(db_value):
|
||||
@ -391,7 +391,7 @@ class Model(metaclass=ModelMeta):
|
||||
db_value = field.to_db_value(ldb, value,
|
||||
FLAG_MOD_REPLACE)
|
||||
except ValueError as e:
|
||||
raise ModelError(e)
|
||||
raise FieldError(e, field=field)
|
||||
|
||||
# When a field returns None or empty list, delete attr.
|
||||
if db_value in (None, []):
|
||||
|
Loading…
x
Reference in New Issue
Block a user