Browse Source

qrscanner: nicer error messages

patch-4
SomberNight 4 years ago
parent
commit
4a8286c744
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 6
      electrum/gui/kivy/main_window.py
  2. 4
      electrum/gui/qt/main_window.py
  3. 6
      electrum/gui/qt/qrtextedit.py
  4. 12
      electrum/qrscanner.py

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

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

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

12
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

Loading…
Cancel
Save