Fix error output with --no-err-windows.

This commit is contained in:
Florian Bruhin 2015-09-04 07:52:55 +02:00
parent 9b1d0af20d
commit 9521da3c73
3 changed files with 56 additions and 18 deletions

View File

@ -21,7 +21,18 @@
from PyQt5.QtWidgets import QMessageBox from PyQt5.QtWidgets import QMessageBox
from qutebrowser.utils import log from qutebrowser.utils import log, utils
def _get_name(exc):
"""Get a suitable exception name as a string."""
prefixes = ['qutebrowser', 'builtins']
name = utils.qualname(exc.__class__)
for prefix in prefixes:
if name.startswith(prefix):
name = name[len(prefix) + 1:]
break
return name
def handle_fatal_exc(exc, args, title, *, pre_text='', post_text=''): def handle_fatal_exc(exc, args, title, *, pre_text='', post_text=''):
@ -39,10 +50,11 @@ def handle_fatal_exc(exc, args, title, *, pre_text='', post_text=''):
""" """
if args.no_err_windows: if args.no_err_windows:
log.misc.exception("Handling fatal {} with --no-err-windows!".format( log.misc.exception("Handling fatal {} with --no-err-windows!".format(
exc.__class__.__name__)) _get_name(exc)))
log.misc.error("title: {}".format(title)) log.misc.error("title: {}".format(title))
log.misc.error("pre_text: {}".format(pre_text)) log.misc.error("pre_text: {}".format(pre_text))
log.misc.error("post_text: {}".format(post_text)) log.misc.error("post_text: {}".format(post_text))
log.misc.error("exception text: {}".format(str(exc) or 'none'))
else: else:
if pre_text: if pre_text:
msg_text = '{}: {}'.format(pre_text, exc) msg_text = '{}: {}'.format(pre_text, exc)

View File

@ -470,6 +470,7 @@ class TestSendOrListen:
@pytest.fixture @pytest.fixture
def qlocalsocket_mock(self, mocker): def qlocalsocket_mock(self, mocker):
m = mocker.patch('qutebrowser.misc.ipc.QLocalSocket', autospec=True) m = mocker.patch('qutebrowser.misc.ipc.QLocalSocket', autospec=True)
m().errorString.return_value = "Error string"
for attr in ['UnknownSocketError', 'UnconnectedState', for attr in ['UnknownSocketError', 'UnconnectedState',
'ConnectionRefusedError', 'ServerNotFoundError', 'ConnectionRefusedError', 'ServerNotFoundError',
'PeerClosedError']: 'PeerClosedError']:
@ -517,12 +518,15 @@ class TestSendOrListen:
msgs = [e.message for e in caplog.records()] msgs = [e.message for e in caplog.records()]
assert "Got AddressInUseError, trying again." in msgs assert "Got AddressInUseError, trying again." in msgs
@pytest.mark.parametrize('has_error, excname', [ @pytest.mark.parametrize('has_error, exc_name, exc_msg', [
(True, 'SocketError'), (True, 'SocketError',
(False, 'AddressInUseError') 'Error while writing to running instance: Error string (error 0)'),
(False, 'AddressInUseError',
'Error while listening to IPC server: Error string (error 8)'),
]) ])
def test_address_in_use_error(self, qlocalserver_mock, qlocalsocket_mock, def test_address_in_use_error(self, qlocalserver_mock, qlocalsocket_mock,
stubs, caplog, args, has_error, excname): stubs, caplog, args, has_error, exc_name,
exc_msg):
"""Test the following scenario: """Test the following scenario:
- First call to send_to_running_instance: - First call to send_to_running_instance:
@ -552,12 +556,14 @@ class TestSendOrListen:
msgs = [e.message for e in caplog.records()] msgs = [e.message for e in caplog.records()]
error_msgs = [ error_msgs = [
'Handling fatal {} with --no-err-windows!'.format(excname), 'Handling fatal misc.ipc.{} with --no-err-windows!'.format(
exc_name),
'title: Error while connecting to running instance!', 'title: Error while connecting to running instance!',
'pre_text: ', 'pre_text: ',
'post_text: Maybe another instance is running but frozen?', 'post_text: Maybe another instance is running but frozen?',
'exception text: {}'.format(exc_msg),
] ]
assert msgs[-4:] == error_msgs assert msgs[-5:] == error_msgs
def test_listen_error(self, qlocalserver_mock, caplog, args): def test_listen_error(self, qlocalserver_mock, caplog, args):
"""Test an error with the first listen call.""" """Test an error with the first listen call."""
@ -571,9 +577,11 @@ class TestSendOrListen:
msgs = [e.message for e in caplog.records()] msgs = [e.message for e in caplog.records()]
error_msgs = [ error_msgs = [
'Handling fatal ListenError with --no-err-windows!', 'Handling fatal misc.ipc.ListenError with --no-err-windows!',
'title: Error while connecting to running instance!', 'title: Error while connecting to running instance!',
'pre_text: ', 'pre_text: ',
'post_text: Maybe another instance is running but frozen?', 'post_text: Maybe another instance is running but frozen?',
'exception text: Error while listening to IPC server: Error '
'string (error 4)',
] ]
assert msgs[-4:] == error_msgs assert msgs[-5:] == error_msgs

View File

@ -25,6 +25,7 @@ import logging
import pytest import pytest
from qutebrowser.utils import error from qutebrowser.utils import error
from qutebrowser.misc import ipc
from PyQt5.QtCore import pyqtSlot, QTimer from PyQt5.QtCore import pyqtSlot, QTimer
from PyQt5.QtWidgets import QMessageBox from PyQt5.QtWidgets import QMessageBox
@ -33,19 +34,36 @@ from PyQt5.QtWidgets import QMessageBox
Args = collections.namedtuple('Args', 'no_err_windows') Args = collections.namedtuple('Args', 'no_err_windows')
def test_no_err_windows(caplog): class TestError(Exception):
"""Test handle_fatal_exc uwith no_err_windows = True."""
pass
@pytest.mark.parametrize('exc, name, exc_text', [
# "builtins." stripped
(ValueError('exception'), 'ValueError', 'exception'),
(ValueError, 'ValueError', 'none'),
# "qutebrowser." stripped
(ipc.Error, 'misc.ipc.Error', 'none'),
(TestError, 'test_error.TestError', 'none'),
])
def test_no_err_windows(caplog, exc, name, exc_text):
"""Test handle_fatal_exc with no_err_windows = True."""
try: try:
raise ValueError("exception") raise exc
except ValueError as e: except Exception as e:
with caplog.atLevel(logging.ERROR): with caplog.atLevel(logging.ERROR):
error.handle_fatal_exc(e, Args(no_err_windows=True), 'title', error.handle_fatal_exc(e, Args(no_err_windows=True), 'title',
pre_text='pre', post_text='post') pre_text='pre', post_text='post')
msgs = [rec.message for rec in caplog.records()] msgs = [rec.message for rec in caplog.records()]
assert msgs[0] == 'Handling fatal ValueError with --no-err-windows!' expected = [
assert msgs[1] == 'title: title' 'Handling fatal {} with --no-err-windows!'.format(name),
assert msgs[2] == 'pre_text: pre' 'title: title',
assert msgs[3] == 'post_text: post' 'pre_text: pre',
'post_text: post',
'exception text: {}'.format(exc_text),
]
assert msgs[-5:] == expected
@pytest.mark.parametrize('pre_text, post_text, expected', [ @pytest.mark.parametrize('pre_text, post_text, expected', [