Use UptimeKumaException exceptions

This commit is contained in:
Pim van Pelt
2025-08-03 11:27:36 +02:00
parent 1e3999eee2
commit f1d10458c6
4 changed files with 22 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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