Display error message when network is down

This commit is contained in:
Wiktor Zykubek 2023-07-11 18:27:45 +02:00
parent 85be796276
commit 8e1cdb76f0
No known key found for this signature in database
GPG Key ID: 0DAA9DC01449FCA2

View File

@ -40,17 +40,20 @@ class MainWindow(QMainWindow):
def getStationsData(self) -> dict: def getStationsData(self) -> dict:
"""Get JSON data from API and convert it to dict.""" """Get JSON data from API and convert it to dict."""
resp = requests.get(API_URL) try:
if resp.status_code not in range(200, 299 + 1): 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( error_box = QMessageBox.critical(
self, self,
"Błąd", "Błąd",
f"Błąd połączenia o kodzie: {resp.status_code}", "Brak połączenia z serwerem.",
QMessageBox.Cancel, QMessageBox.Cancel,
) )
error_box.exec() error_box.exec()
else:
return json.loads(resp.text)
def printGroups(self) -> None: def printGroups(self) -> None:
"""Print groups (categories) in groupsListWidget.""" """Print groups (categories) in groupsListWidget."""