electrum

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

commit 4a8286c744ab542d9893f202468ec86a171af791
parent 22a14d42b2e44e9adcfb6971b88e31500594dacb
Author: SomberNight <somber.night@protonmail.com>
Date:   Tue, 23 Feb 2021 22:38:53 +0100

qrscanner: nicer error messages

Diffstat:
Melectrum/gui/kivy/main_window.py | 6+++++-
Melectrum/gui/qt/main_window.py | 4++++
Melectrum/gui/qt/qrtextedit.py | 6+++++-
Melectrum/qrscanner.py | 12++++++++----
4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/electrum/gui/kivy/main_window.py b/electrum/gui/kivy/main_window.py @@ -81,7 +81,8 @@ Label.register( from electrum.util import (NoDynamicFeeEstimates, NotEnoughFunds, - BITCOIN_BIP21_URI_SCHEME, LIGHTNING_URI_SCHEME) + BITCOIN_BIP21_URI_SCHEME, LIGHTNING_URI_SCHEME, + UserFacingException) from .uix.dialogs.lightning_open_channel import LightningOpenChannelDialog from .uix.dialogs.lightning_channels import LightningChannelsDialog, SwapDialog @@ -545,7 +546,10 @@ class ElectrumWindow(App, Logger): video_dev = self.electrum_config.get_video_device() data = qrscanner.scan_barcode(video_dev) on_complete(data) + except UserFacingException as e: + self.show_error(e) except BaseException as e: + self.logger.exception('camera error') self.show_error(repr(e)) def do_share(self, data, title): diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py @@ -2701,7 +2701,11 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): from electrum import qrscanner try: data = qrscanner.scan_barcode(self.config.get_video_device()) + except UserFacingException as e: + self.show_error(e) + return except BaseException as e: + self.logger.exception('camera error') self.show_error(repr(e)) return if not data: diff --git a/electrum/gui/qt/qrtextedit.py b/electrum/gui/qt/qrtextedit.py @@ -3,6 +3,7 @@ from PyQt5.QtWidgets import QFileDialog from electrum.i18n import _ from electrum.plugin import run_hook from electrum.simple_config import SimpleConfig +from electrum.util import UserFacingException from .util import ButtonsTextEdit, MessageBoxMixin, ColorScheme, getOpenFileName @@ -71,11 +72,14 @@ class ScanQRTextEdit(ButtonsTextEdit, MessageBoxMixin): def qr_input(self): from electrum import qrscanner + data = '' try: data = qrscanner.scan_barcode(self.config.get_video_device()) + except UserFacingException as e: + self.show_error(e) except BaseException as e: + self.logger.exception('camera error') self.show_error(repr(e)) - data = '' if not data: data = '' if self.allow_multi: diff --git a/electrum/qrscanner.py b/electrum/qrscanner.py @@ -27,6 +27,8 @@ import os import sys import ctypes +from .util import UserFacingException +from .i18n import _ from .logging import get_logger @@ -53,7 +55,7 @@ except BaseException as e1: def scan_barcode_ctypes(device='', timeout=-1, display=True, threaded=False): if libzbar is None: - raise RuntimeError("Cannot start QR scanner; zbar not available.") + raise UserFacingException("Cannot start QR scanner: zbar not available.") libzbar.zbar_symbol_get_data.restype = ctypes.c_char_p libzbar.zbar_processor_create.restype = ctypes.POINTER(ctypes.c_int) libzbar.zbar_processor_get_results.restype = ctypes.POINTER(ctypes.c_int) @@ -62,7 +64,9 @@ def scan_barcode_ctypes(device='', timeout=-1, display=True, threaded=False): proc = libzbar.zbar_processor_create(threaded) libzbar.zbar_processor_request_size(proc, 640, 480) if libzbar.zbar_processor_init(proc, device.encode('utf-8'), display) != 0: - raise RuntimeError("Can not start QR scanner; initialization failed.") + raise UserFacingException( + _("Cannot start QR scanner: initialization failed.") + "\n" + + _("Make sure you have a camera connected and enabled.")) libzbar.zbar_processor_set_visible(proc) if libzbar.zbar_process_one(proc, timeout): symbols = libzbar.zbar_processor_get_results(proc) @@ -85,7 +89,7 @@ def scan_barcode_osx(*args_ignored, **kwargs_ignored): root_ec_dir = os.path.abspath(os.path.dirname(__file__) + "/../") prog = root_ec_dir + "/" + "contrib/osx/CalinsQRReader/build/Release/CalinsQRReader.app/Contents/MacOS/CalinsQRReader" if not os.path.exists(prog): - raise RuntimeError("Cannot start QR scanner; helper app not found.") + raise UserFacingException("Cannot start QR scanner: helper app not found.") data = '' try: # This will run the "CalinsQRReader" helper app (which also gets bundled with the built .app) @@ -96,7 +100,7 @@ def scan_barcode_osx(*args_ignored, **kwargs_ignored): data = p.stdout.read().decode('utf-8').strip() return data except OSError as e: - raise RuntimeError("Cannot start camera helper app; {}".format(e.strerror)) + raise UserFacingException("Cannot start camera helper app: {}".format(e.strerror)) scan_barcode = scan_barcode_osx if sys.platform == 'darwin' else scan_barcode_ctypes