From 8b8b95d555acfd2fec339ba39f1de1af912897e1 Mon Sep 17 00:00:00 2001
From: Karel van Klink <karel.vanklink@geant.org>
Date: Fri, 18 Oct 2024 11:29:26 +0200
Subject: [PATCH] Improve error handling for missing LibreNMS device in router
 termination

---
 gso/workflows/router/terminate_router.py | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/gso/workflows/router/terminate_router.py b/gso/workflows/router/terminate_router.py
index ad374f23..56f927d8 100644
--- a/gso/workflows/router/terminate_router.py
+++ b/gso/workflows/router/terminate_router.py
@@ -8,6 +8,7 @@ from orchestrator.forms import FormPage
 from orchestrator.forms.validators import Label
 from orchestrator.targets import Target
 from orchestrator.types import FormGenerator, State, SubscriptionLifecycle, UUIDstr
+from orchestrator.utils.errors import ProcessFailureError
 from orchestrator.utils.json import json_dumps
 from orchestrator.workflow import StepList, begin, conditional, done, step, workflow
 from orchestrator.workflows.steps import (
@@ -223,13 +224,19 @@ def remove_pe_from_all_p_real(subscription: Router, tt_number: str, process_id:
 
 
 @step("Remove Device from Librenms")
-def remove_device_from_librenms(subscription: Router) -> State | None:
+def remove_device_from_librenms(subscription: Router) -> State:
     """Remove the device from LibreNMS."""
     try:
         LibreNMSClient().remove_device(subscription.router.router_fqdn)
     except HTTPError as e:
-        return {"librenms_error": str(e)}
-    return None
+        response = e.response.json()
+        if response["message"] == f"Device {subscription.router.router_fqdn} not found":
+            return {
+                "librenms_error": str(e.response.json()),
+                "librenms_device": "Device not found, please confirm nothing needs to be removed from LibreNMS",
+            }
+        raise ProcessFailureError(message="LibreNMS error", details=response) from None
+    return {"librenms_device": "Device removed from LibreNMS"}
 
 
 @step("Apply the archiving license in Kentik")
-- 
GitLab