From bce609f0742fce64c68fd69055ad3957fb597705 Mon Sep 17 00:00:00 2001 From: Florian Bussmann Date: Wed, 20 Aug 2025 23:46:22 +0200 Subject: [PATCH] fix: throw 408 if alertmanager instance unreachable --- lib/main.dart | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index f7f749c..6e675e7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -118,7 +118,14 @@ class _AlertListScreenState extends State { Future _fetchAlerts() async { try { final url = Uri.parse('$baseUrl/api/v2/alerts'); - final response = await http.get(url); + final response = await http + .get(url) + .timeout( + const Duration(seconds: 5), + onTimeout: () { + return http.Response("", 408); + }, + ); if (response.statusCode == 200) { final List jsonData = jsonDecode(response.body); @@ -129,13 +136,42 @@ class _AlertListScreenState extends State { _alerts = jsonData.map((e) => Alert.fromJson(e)).toList(); }); } else { - throw Exception('Failed to load alerts: ${response.statusCode}'); + _showConnectionError( + 'Failed to connect to $baseUrl with status ${response.statusCode}. Please check the URL.', + _fetchAlerts, + ); } } catch (_) { _promptForUrl(); } } + void _showConnectionError(String message, VoidCallback onRetry) { + showDialog( + context: context, + builder: (context) => AlertDialog( + title: const Text('Connection Error'), + content: Text(message), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + _promptForUrl(); + }, + child: const Text('Enter URL'), + ), + ElevatedButton( + onPressed: () { + Navigator.of(context).pop(); + onRetry(); + }, + child: const Text('Try Again'), + ), + ], + ), + ); + } + Map> _groupBySeverity(List alerts) { final Map> grouped = {}; for (var alert in alerts) {