electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 909c063eb1922d0a7e4b61d5538cb5b34edbf1e7
parent 7f04c305676df739b8ba00137df22011f9477488
Author: SomberNight <somber.night@protonmail.com>
Date:   Wed, 14 Feb 2018 19:42:35 +0100

contact/invoice import: better exception handling.

see #3904

Diffstat:
Mgui/qt/contact_list.py | 7+++++--
Mgui/qt/invoice_list.py | 7+++++--
Mlib/contacts.py | 11+++++++++--
Mlib/paymentrequest.py | 8++++++--
Mlib/util.py | 13+++++++++++++
5 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/gui/qt/contact_list.py b/gui/qt/contact_list.py @@ -26,7 +26,7 @@ import webbrowser from electrum.i18n import _ from electrum.bitcoin import is_address -from electrum.util import block_explorer_URL +from electrum.util import block_explorer_URL, FileImportFailed from electrum.plugins import run_hook from PyQt5.QtGui import * from PyQt5.QtCore import * @@ -57,7 +57,10 @@ class ContactList(MyTreeWidget): filename, __ = QFileDialog.getOpenFileName(self.parent, "Select your wallet file", wallet_folder) if not filename: return - self.parent.contacts.import_file(filename) + try: + self.parent.contacts.import_file(filename) + except FileImportFailed as e: + self.parent.show_message(str(e)) self.on_update() def create_menu(self, position): diff --git a/gui/qt/invoice_list.py b/gui/qt/invoice_list.py @@ -25,7 +25,7 @@ from .util import * from electrum.i18n import _ -from electrum.util import format_time +from electrum.util import format_time, FileImportFailed class InvoiceList(MyTreeWidget): @@ -61,7 +61,10 @@ class InvoiceList(MyTreeWidget): filename, __ = QFileDialog.getOpenFileName(self.parent, "Select your wallet file", wallet_folder) if not filename: return - self.parent.invoices.import_file(filename) + try: + self.parent.invoices.import_file(filename) + except FileImportFailed as e: + self.parent.show_message(str(e)) self.on_update() def create_menu(self, position): diff --git a/lib/contacts.py b/lib/contacts.py @@ -23,9 +23,12 @@ import re import dns import json +import traceback +import sys from . import bitcoin from . import dnssec +from .util import FileImportFailed, FileImportFailedEncrypted class Contacts(dict): @@ -51,8 +54,12 @@ class Contacts(dict): try: with open(path, 'r') as f: d = self._validate(json.loads(f.read())) - except: - return + except json.decoder.JSONDecodeError: + traceback.print_exc(file=sys.stderr) + raise FileImportFailedEncrypted() + except BaseException: + traceback.print_exc(file=sys.stdout) + raise FileImportFailed() self.update(d) self.save() diff --git a/lib/paymentrequest.py b/lib/paymentrequest.py @@ -40,6 +40,7 @@ except ImportError: from . import bitcoin from . import util from .util import print_error, bh2u, bfh +from .util import FileImportFailed, FileImportFailedEncrypted from . import transaction from . import x509 from . import rsakey @@ -471,9 +472,12 @@ class InvoiceStore(object): with open(path, 'r') as f: d = json.loads(f.read()) self.load(d) - except: + except json.decoder.JSONDecodeError: traceback.print_exc(file=sys.stderr) - return + raise FileImportFailedEncrypted() + except BaseException: + traceback.print_exc(file=sys.stdout) + raise FileImportFailed() self.save() def save(self): diff --git a/lib/util.py b/lib/util.py @@ -58,6 +58,19 @@ class InvalidPassword(Exception): def __str__(self): return _("Incorrect password") + +class FileImportFailed(Exception): + def __str__(self): + return _("Failed to import file.") + + +class FileImportFailedEncrypted(FileImportFailed): + def __str__(self): + return (_('Failed to import file.') + ' ' + + _('Perhaps it is encrypted...') + '\n' + + _('Importing encrypted files is not supported.')) + + # Throw this exception to unwind the stack like when an error occurs. # However unlike other exceptions the user won't be informed. class UserCancelled(Exception):