From 987a1a3fd8053c11b560ccb48ad1510a8354ec2f Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sun, 25 Jan 2015 15:38:36 +0100 Subject: [PATCH] Catching RequestsExceptions --- src/cutecoin/core/community.py | 7 +++---- src/cutecoin/gui/currency_tab.py | 7 ++++++- src/cutecoin/gui/mainwindow.py | 5 +++++ src/cutecoin/gui/process_cfg_community.py | 24 +++++++++++++++++++++-- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index c60ae4b2..cef3bf7c 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -12,7 +12,7 @@ from ..tools.exceptions import NoPeerAvailable import logging import inspect import hashlib -from requests.exceptions import ConnectTimeout +from requests.exceptions import RequestException, ConnectTimeout class Cache(): @@ -128,12 +128,12 @@ class Community(object): (next_peer.pubkey not in traversed_pubkeys))) if next_peer.pubkey not in traversed_pubkeys: self._peering_traversal(next_peer, found_peers, traversed_pubkeys) - except ConnectTimeout: - pass except TimeoutError: pass except ValueError: pass + except RequestException as e: + pass def peering(self): peers = [] @@ -218,7 +218,6 @@ class Community(object): self.peers.remove(peer) self.peers.append(peer) continue - raise NoPeerAvailable(self.currency, len(self.peers)) def post(self, request, req_args={}, post_args={}): diff --git a/src/cutecoin/gui/currency_tab.py b/src/cutecoin/gui/currency_tab.py index ac552f8a..0bd4a8d9 100644 --- a/src/cutecoin/gui/currency_tab.py +++ b/src/cutecoin/gui/currency_tab.py @@ -6,9 +6,10 @@ Created on 2 févr. 2014 import logging import time +import requests from ucoinpy.api import bma -from PyQt5.QtWidgets import QWidget, QMenu, QAction, QApplication +from PyQt5.QtWidgets import QWidget, QMenu, QAction, QApplication, QMessageBox from PyQt5.QtCore import QModelIndex, Qt, pyqtSlot, QObject, QThread, pyqtSignal from PyQt5.QtGui import QIcon from ..gen_resources.currency_tab_uic import Ui_CurrencyTabWidget @@ -47,6 +48,10 @@ class BlockchainWatcher(QObject): self.last_block = block_number except NoPeerAvailable: return + except requests.exceptions.RequestException as e: + QMessageBox.critical(self, ":(", + str(e), + QMessageBox.Ok) new_block_mined = pyqtSignal(int) diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py index 4bb208c9..4a8f05d5 100644 --- a/src/cutecoin/gui/mainwindow.py +++ b/src/cutecoin/gui/mainwindow.py @@ -18,6 +18,7 @@ from ..tools.exceptions import NoPeerAvailable from ..__init__ import __version__ import logging +import requests class Loader(QObject): @@ -189,6 +190,10 @@ class MainWindow(QMainWindow, Ui_MainWindow): str(e), QMessageBox.Ok) continue + except requests.exceptions.RequestException as e: + QMessageBox.critical(self, ":(", + str(e), + QMessageBox.Ok) self.menu_contacts_list.clear() for contact in self.app.current_account.contacts: diff --git a/src/cutecoin/gui/process_cfg_community.py b/src/cutecoin/gui/process_cfg_community.py index e3f75e11..ac25c61b 100644 --- a/src/cutecoin/gui/process_cfg_community.py +++ b/src/cutecoin/gui/process_cfg_community.py @@ -5,6 +5,8 @@ Created on 8 mars 2014 ''' import logging +import requests + from ucoinpy.api import bma from ucoinpy.api.bma import ConnectionHandler from ucoinpy.documents.peer import Peer @@ -65,6 +67,11 @@ class StepPageInit(Step): QMessageBox.critical(self.config_dialog, "Server Error", "Cannot join any peer in this community.") raise + except requests.exceptions.RequestException as e: + QMessageBox.critical(self, ":(", + str(e), + QMessageBox.Ok) + raise def display_page(self): self.config_dialog.button_previous.setEnabled(False) @@ -155,8 +162,12 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog): peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'], peer_data['signature'])) - self.community.peers.append(peer) - except: + if peer.currency == self.community.currency: + self.community.peers.append(peer) + else: + QMessageBox.critical(self, "Error", + "This peer doesn't use this community currency.") + except requests.exceptions.RequestException as e: QMessageBox.critical(self, "Server error", "Cannot get node peering") self.tree_peers.setModel(PeeringTreeModel(self.community)) @@ -187,6 +198,15 @@ Would you like to publish the key ?""".format(self.account.pubkey)) except ValueError as e: QMessageBox.critical(self, "Pubkey publishing error", e.message) + except NoPeerAvailable as e: + QMessageBox.critical(self, "Network error", + "Couldn't connect to network : {0}".format(e), + QMessageBox.Ok) + except Exception as e: + QMessageBox.critical(self, "Error", + "{0}".format(e), + QMessageBox.Ok) + else: return -- GitLab