Browse Source

Merge pull request #4094 from SomberNight/labels_plugin_catch_exc

labels plugin: better exception handling
3.2.x
ThomasV 7 years ago
committed by GitHub
parent
commit
382f69edd9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      plugins/labels/labels.py
  2. 21
      plugins/labels/qt.py

33
plugins/labels/labels.py

@ -45,7 +45,7 @@ class LabelsPlugin(BasePlugin):
@hook @hook
def set_label(self, wallet, item, label): def set_label(self, wallet, item, label):
if not wallet in self.wallets: if wallet not in self.wallets:
return return
if not item: if not item:
return return
@ -55,7 +55,7 @@ class LabelsPlugin(BasePlugin):
"walletNonce": nonce, "walletNonce": nonce,
"externalId": self.encode(wallet, item), "externalId": self.encode(wallet, item),
"encryptedLabel": self.encode(wallet, label)} "encryptedLabel": self.encode(wallet, label)}
t = threading.Thread(target=self.do_request, t = threading.Thread(target=self.do_request_safe,
args=["POST", "/label", False, bundle]) args=["POST", "/label", False, bundle])
t.setDaemon(True) t.setDaemon(True)
t.start() t.start()
@ -78,8 +78,18 @@ class LabelsPlugin(BasePlugin):
raise BaseException(response["error"]) raise BaseException(response["error"])
return response return response
def do_request_safe(self, *args, **kwargs):
try:
self.do_request(*args, **kwargs)
except BaseException as e:
#traceback.print_exc(file=sys.stderr)
self.print_error('error doing request')
def push_thread(self, wallet): def push_thread(self, wallet):
wallet_id = self.wallets[wallet][2] wallet_data = self.wallets.get(wallet, None)
if not wallet_data:
raise Exception('Wallet {} not loaded'.format(wallet))
wallet_id = wallet_data[2]
bundle = {"labels": [], bundle = {"labels": [],
"walletId": wallet_id, "walletId": wallet_id,
"walletNonce": self.get_nonce(wallet)} "walletNonce": self.get_nonce(wallet)}
@ -95,10 +105,12 @@ class LabelsPlugin(BasePlugin):
self.do_request("POST", "/labels", True, bundle) self.do_request("POST", "/labels", True, bundle)
def pull_thread(self, wallet, force): def pull_thread(self, wallet, force):
wallet_id = self.wallets[wallet][2] wallet_data = self.wallets.get(wallet, None)
if not wallet_data:
raise Exception('Wallet {} not loaded'.format(wallet))
wallet_id = wallet_data[2]
nonce = 1 if force else self.get_nonce(wallet) - 1 nonce = 1 if force else self.get_nonce(wallet) - 1
self.print_error("asking for labels since nonce", nonce) self.print_error("asking for labels since nonce", nonce)
try:
response = self.do_request("GET", ("/labels/since/%d/for/%s" % (nonce, wallet_id) )) response = self.do_request("GET", ("/labels/since/%d/for/%s" % (nonce, wallet_id) ))
if response["labels"] is None: if response["labels"] is None:
self.print_error('no new labels') self.print_error('no new labels')
@ -128,9 +140,12 @@ class LabelsPlugin(BasePlugin):
self.set_nonce(wallet, response["nonce"] + 1) self.set_nonce(wallet, response["nonce"] + 1)
self.on_pulled(wallet) self.on_pulled(wallet)
except Exception as e: def pull_thread_safe(self, wallet, force):
traceback.print_exc(file=sys.stderr) try:
self.print_error("could not retrieve labels") self.pull_thread(wallet, force)
except BaseException as e:
# traceback.print_exc(file=sys.stderr)
self.print_error('could not retrieve labels')
def start_wallet(self, wallet): def start_wallet(self, wallet):
nonce = self.get_nonce(wallet) nonce = self.get_nonce(wallet)
@ -144,7 +159,7 @@ class LabelsPlugin(BasePlugin):
wallet_id = hashlib.sha256(mpk).hexdigest() wallet_id = hashlib.sha256(mpk).hexdigest()
self.wallets[wallet] = (password, iv, wallet_id) self.wallets[wallet] = (password, iv, wallet_id)
# If there is an auth token we can try to actually start syncing # If there is an auth token we can try to actually start syncing
t = threading.Thread(target=self.pull_thread, args=(wallet, False)) t = threading.Thread(target=self.pull_thread_safe, args=(wallet, False))
t.setDaemon(True) t.setDaemon(True)
t.start() t.start()

21
plugins/labels/qt.py

@ -1,4 +1,6 @@
from functools import partial from functools import partial
import traceback
import sys
from PyQt5.QtGui import * from PyQt5.QtGui import *
from PyQt5.QtCore import * from PyQt5.QtCore import *
@ -37,10 +39,12 @@ class Plugin(LabelsPlugin):
hbox.addWidget(QLabel("Label sync options:")) hbox.addWidget(QLabel("Label sync options:"))
upload = ThreadedButton("Force upload", upload = ThreadedButton("Force upload",
partial(self.push_thread, wallet), partial(self.push_thread, wallet),
partial(self.done_processing, d)) partial(self.done_processing_success, d),
partial(self.done_processing_error, d))
download = ThreadedButton("Force download", download = ThreadedButton("Force download",
partial(self.pull_thread, wallet, True), partial(self.pull_thread, wallet, True),
partial(self.done_processing, d)) partial(self.done_processing_success, d),
partial(self.done_processing_error, d))
vbox = QVBoxLayout() vbox = QVBoxLayout()
vbox.addWidget(upload) vbox.addWidget(upload)
vbox.addWidget(download) vbox.addWidget(download)
@ -54,13 +58,20 @@ class Plugin(LabelsPlugin):
def on_pulled(self, wallet): def on_pulled(self, wallet):
self.obj.labels_changed_signal.emit(wallet) self.obj.labels_changed_signal.emit(wallet)
def done_processing(self, dialog, result): def done_processing_success(self, dialog, result):
dialog.show_message(_("Your labels have been synchronised.")) dialog.show_message(_("Your labels have been synchronised."))
def done_processing_error(self, dialog, result):
traceback.print_exception(*result, file=sys.stderr)
dialog.show_error(_("Error synchronising labels") + ':\n' + str(result[:2]))
@hook @hook
def on_new_window(self, window): def load_wallet(self, wallet, window):
# FIXME if the user just enabled the plugin, this hook won't be called
# as the wallet is already loaded, and hence the plugin will be in
# a non-functional state for that window
self.obj.labels_changed_signal.connect(window.update_tabs) self.obj.labels_changed_signal.connect(window.update_tabs)
self.start_wallet(window.wallet) self.start_wallet(wallet)
@hook @hook
def on_close_window(self, window): def on_close_window(self, window):

Loading…
Cancel
Save