Browse Source

contact/invoice import: better exception handling.

see #3904
3.1
SomberNight 7 years ago
parent
commit
909c063eb1
  1. 7
      gui/qt/contact_list.py
  2. 7
      gui/qt/invoice_list.py
  3. 11
      lib/contacts.py
  4. 8
      lib/paymentrequest.py
  5. 13
      lib/util.py

7
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):

7
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):

11
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()

8
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):

13
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):

Loading…
Cancel
Save