From 7c09eb298ddf015eedf0454bd21b551ad1618bd2 Mon Sep 17 00:00:00 2001 From: Daniel Sheppard Date: Tue, 1 Aug 2023 13:01:36 -0500 Subject: [PATCH] Catch AssertionError's in signals. Handle accordingly --- netbox/dcim/models/cables.py | 4 ++++ netbox/dcim/signals.py | 6 +++++- netbox/dcim/utils.py | 10 +++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/netbox/dcim/models/cables.py b/netbox/dcim/models/cables.py index af69c440e..dbf6f770b 100644 --- a/netbox/dcim/models/cables.py +++ b/netbox/dcim/models/cables.py @@ -478,8 +478,11 @@ class CablePath(models.Model): Cable or WirelessLink connects (interfaces, console ports, circuit termination, etc.). All terminations must be of the same type and must belong to the same parent object. """ + import logging from circuits.models import CircuitTermination + logger = logging.getLogger('netbox.dcim.cablepath') + if not terminations: return None @@ -558,6 +561,7 @@ class CablePath(models.Model): pk__in=[t.rear_port_id for t in remote_terminations] ) if len(rear_ports) > 1: + logger.warning(f'All rear-port positions do not match. Cannot continue path trace.') assert all(rp.positions == 1 for rp in rear_ports) elif rear_ports[0].positions > 1: position_stack.append([fp.rear_port_position for fp in remote_terminations]) diff --git a/netbox/dcim/signals.py b/netbox/dcim/signals.py index a51872719..4aae71583 100644 --- a/netbox/dcim/signals.py +++ b/netbox/dcim/signals.py @@ -95,7 +95,11 @@ def update_connected_endpoints(instance, created, raw=False, **kwargs): if not nodes: continue if isinstance(nodes[0], PathEndpoint): - create_cablepath(nodes) + try: + create_cablepath(nodes) + except AssertionError: + # This is likely an unsupported path. Catch the assertion error and don't save the path + logger.error(f'Unsupported path from nodes: {[node.name for node in nodes].join(",")}') else: rebuild_paths(nodes) diff --git a/netbox/dcim/utils.py b/netbox/dcim/utils.py index eadd2da96..5740ddcc7 100644 --- a/netbox/dcim/utils.py +++ b/netbox/dcim/utils.py @@ -49,12 +49,20 @@ def rebuild_paths(terminations): """ Rebuild all CablePaths which traverse the specified nodes. """ + import logging from dcim.models import CablePath + logger = logging.getLogger('netbox.dcim.cable') + for obj in terminations: cable_paths = CablePath.objects.filter(_nodes__contains=obj) with transaction.atomic(): for cp in cable_paths: cp.delete() - create_cablepath(cp.origins) + try: + create_cablepath(cp.origins) + except AssertionError: + # This is likely an unsupported path. Catch the assertion error and don't save the path + logger.error(f'Unsupported path from cable path: {cp._nodes}') + pass