Skip to content

Commit

Permalink
Catch Django ValidationError when creating/updating Nautobot data. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmatthews committed Mar 5, 2021
1 parent 6da2ee3 commit eeec890
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions nautobot_netbox_importer/diffsync/models/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from diffsync import DiffSync, DiffSyncModel
from diffsync.exceptions import ObjectNotFound
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db import models
from django.db.utils import IntegrityError
from pydantic import BaseModel, validator
Expand Down Expand Up @@ -183,8 +184,22 @@ def create_nautobot_record(nautobot_model, ids: Mapping, attrs: Mapping, multiva
getattr(record, attr).set(value)
return record
except IntegrityError as exc:
logger.error(f"Error in creating {nautobot_model}: {exc}")
return None
logger.error(
"Nautobot reported a database integrity error",
action="create",
exception=str(exc),
model=nautobot_model,
model_data=dict(**ids, **attrs, **multivalue_attrs)
)
except DjangoValidationError as exc:
logger.error(
"Nautobot reported a data validation error - check your source data",
action="create",
exception=str(exc),
model=nautobot_model,
model_data=dict(**ids, **attrs, **multivalue_attrs),
)
return None

@classmethod
def create(cls, diffsync: DiffSync, ids: Mapping, attrs: Mapping) -> Optional["NautobotBaseModel"]:
Expand Down Expand Up @@ -219,8 +234,22 @@ def update_nautobot_record(nautobot_model, ids: Mapping, attrs: Mapping, multiva
getattr(record, attr).set(value)
return record
except IntegrityError as exc:
logger.error(f"Error in updating {nautobot_model}: {exc}")
return None
logger.error(
"Nautobot reported a database integrity error",
action="update",
exception=str(exc),
model=nautobot_model,
model_data=dict(**ids, **attrs, **multivalue_attrs),
)
except DjangoValidationError as exc:
logger.error(
"Nautobot reported a data validation error - check your source data",
action="update",
exception=str(exc),
model=nautobot_model,
model_data=dict(**ids, **attrs, **multivalue_attrs),
)
return None

def update(self, attrs: Mapping) -> Optional["NautobotBaseModel"]:
"""Update this model instance, both in Nautobot and in DiffSync."""
Expand Down

0 comments on commit eeec890

Please sign in to comment.