From f1d10458c6230af235396840752562f64f6d642f Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sun, 3 Aug 2025 11:27:36 +0200 Subject: [PATCH] Use UptimeKumaException exceptions --- src/kumacli/client.py | 14 +++++++------- src/kumacli/cmd/info.py | 3 ++- src/kumacli/cmd/maintenance.py | 13 +++++++------ src/kumacli/cmd/monitor.py | 11 ++++++----- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/kumacli/client.py b/src/kumacli/client.py index d5d2456..776b406 100644 --- a/src/kumacli/client.py +++ b/src/kumacli/client.py @@ -4,7 +4,7 @@ import fnmatch import re from datetime import datetime -from uptime_kuma_api import UptimeKumaApi +from uptime_kuma_api import UptimeKumaApi, UptimeKumaException class KumaClient: @@ -77,7 +77,7 @@ class KumaClient: self.api.login(self.username, self.password) print(f"Connected to {self.url}") return True - except Exception as e: + except UptimeKumaException as e: print(f"Failed to connect: {e}") return False @@ -111,7 +111,7 @@ class KumaClient: return unique_monitors - except Exception as e: + except UptimeKumaException as e: print(f"Error finding monitors: {e}") return [] @@ -141,7 +141,7 @@ class KumaClient: return unique_groups - except Exception as e: + except UptimeKumaException as e: print(f"Error finding groups: {e}") return [] @@ -168,7 +168,7 @@ class KumaClient: return group_members - except Exception as e: + except UptimeKumaException as e: print(f"Error getting group members: {e}") return [] @@ -226,7 +226,7 @@ class KumaClient: # Return list of monitor IDs return [monitor["id"] for monitor in matched_monitors] - except Exception as e: + except UptimeKumaException as e: print(f"Error finding monitors by globs: {e}") return [] @@ -255,7 +255,7 @@ class KumaClient: } for mid in monitor_ids ] - except Exception as e: + except UptimeKumaException as e: print(f"Error getting monitor details: {e}") return [] diff --git a/src/kumacli/cmd/info.py b/src/kumacli/cmd/info.py index e651647..dbd819c 100644 --- a/src/kumacli/cmd/info.py +++ b/src/kumacli/cmd/info.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """Info command implementations for Uptime Kuma CLI.""" +from uptime_kuma_api import UptimeKumaException from ..client import KumaClient @@ -22,7 +23,7 @@ class InfoCommands: for key, value in info.items(): print(f" {key}: {value}") - except Exception as e: + except UptimeKumaException as e: print(f"Error getting server info: {e}") diff --git a/src/kumacli/cmd/maintenance.py b/src/kumacli/cmd/maintenance.py index 3f932c9..ad81ab0 100644 --- a/src/kumacli/cmd/maintenance.py +++ b/src/kumacli/cmd/maintenance.py @@ -2,6 +2,7 @@ """Maintenance command implementations for Uptime Kuma CLI.""" from datetime import datetime, timedelta +from uptime_kuma_api import UptimeKumaException from ..client import KumaClient @@ -35,7 +36,7 @@ class MaintenanceCommands: f"{maintenance_id:<5} {title:<30} {strategy:<15} {active:<10} {description:<50}" ) - except Exception as e: + except UptimeKumaException as e: print(f"Error listing maintenances: {e}") def add_maintenance( @@ -104,13 +105,13 @@ class MaintenanceCommands: f"Successfully added {len(matched_monitors)} monitors to maintenance" ) print(f"API response: {result}") - except Exception as e: + except UptimeKumaException as e: print(f"Error: Failed to add monitors to maintenance: {e}") print( "This might be due to API compatibility issues or server configuration" ) - except Exception as e: + except UptimeKumaException as e: print(f"Error adding maintenance: {e}") def delete_maintenance(self, maintenance_id=None, delete_all=False): @@ -142,7 +143,7 @@ class MaintenanceCommands: f"(ID: {maintenance.get('id')})" ) deleted_count += 1 - except Exception as e: + except UptimeKumaException as e: print( f"Failed to delete maintenance '{maintenance.get('title', 'N/A')}': {e}" ) @@ -165,14 +166,14 @@ class MaintenanceCommands: ) print(f"API response: {result}") - except Exception as e: + except UptimeKumaException as e: print(f"Failed to delete maintenance ID {maintenance_id}: {e}") else: print( "Error: Either --id or --all flag is required for delete operation" ) - except Exception as e: + except UptimeKumaException as e: print(f"Error during maintenance deletion: {e}") diff --git a/src/kumacli/cmd/monitor.py b/src/kumacli/cmd/monitor.py index fe43a4d..1e7dc76 100644 --- a/src/kumacli/cmd/monitor.py +++ b/src/kumacli/cmd/monitor.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """Monitor command implementations for Uptime Kuma CLI.""" +from uptime_kuma_api import UptimeKumaException from ..client import KumaClient @@ -68,7 +69,7 @@ class MonitorCommands: f"{parent_name:<20} {url:<35} {active:<10}" ) - except Exception as e: + except UptimeKumaException as e: print(f"Error listing monitors: {e}") def pause_monitors(self, monitor_patterns=None, group_patterns=None): @@ -91,14 +92,14 @@ class MonitorCommands: self.client.api.pause_monitor(monitor["id"]) print(f"Paused monitor '{monitor['name']}' (ID: {monitor['id']})") paused_count += 1 - except Exception as e: + except UptimeKumaException as e: print(f"Failed to pause monitor '{monitor['name']}': {e}") print( f"Successfully paused {paused_count} out of {len(matched_monitors)} monitors" ) - except Exception as e: + except UptimeKumaException as e: print(f"Error pausing monitors: {e}") def resume_monitors( @@ -147,14 +148,14 @@ class MonitorCommands: self.client.api.resume_monitor(monitor["id"]) print(f"Resumed monitor '{monitor['name']}' (ID: {monitor['id']})") resumed_count += 1 - except Exception as e: + except UptimeKumaException as e: print(f"Failed to resume monitor '{monitor['name']}': {e}") print( f"Successfully resumed {resumed_count} out of {len(matched_monitors)} monitors" ) - except Exception as e: + except UptimeKumaException as e: print(f"Error resuming monitors: {e}")