Catch AssertionError's in signals. Handle accordingly

This commit is contained in:
Daniel Sheppard 2023-08-01 13:01:36 -05:00
parent 0b10131564
commit 7c09eb298d
3 changed files with 18 additions and 2 deletions

View File

@ -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])

View File

@ -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)

View File

@ -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