From 8e1cdb76f0330a9699989fe08875cbe1ce742b53 Mon Sep 17 00:00:00 2001 From: Wiktor Zykubek Date: Tue, 11 Jul 2023 18:27:45 +0200 Subject: [PATCH] Display error message when network is down --- openfm_qt/mainwindow.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/openfm_qt/mainwindow.py b/openfm_qt/mainwindow.py index 741d3f4..f72174d 100644 --- a/openfm_qt/mainwindow.py +++ b/openfm_qt/mainwindow.py @@ -40,17 +40,20 @@ class MainWindow(QMainWindow): def getStationsData(self) -> dict: """Get JSON data from API and convert it to dict.""" - resp = requests.get(API_URL) - if resp.status_code not in range(200, 299 + 1): + try: + resp = requests.get(API_URL) + if resp.status_code not in range(200, 299 + 1): + raise requests.exceptions.ConnectionError() + else: + return json.loads(resp.text) + except requests.exceptions.ConnectionError: error_box = QMessageBox.critical( self, "Błąd", - f"Błąd połączenia o kodzie: {resp.status_code}", + "Brak połączenia z serwerem.", QMessageBox.Cancel, ) error_box.exec() - else: - return json.loads(resp.text) def printGroups(self) -> None: """Print groups (categories) in groupsListWidget."""