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 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=''):
@ -39,10 +50,11 @@ def handle_fatal_exc(exc, args, title, *, pre_text='', post_text=''):
"""
if args.no_err_windows:
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("pre_text: {}".format(pre_text))
log.misc.error("post_text: {}".format(post_text))
log.misc.error("exception text: {}".format(str(exc) or 'none'))
else:
if pre_text:
msg_text = '{}: {}'.format(pre_text, exc)

View File

@ -470,6 +470,7 @@ class TestSendOrListen:
@pytest.fixture
def qlocalsocket_mock(self, mocker):
m = mocker.patch('qutebrowser.misc.ipc.QLocalSocket', autospec=True)
m().errorString.return_value = "Error string"
for attr in ['UnknownSocketError', 'UnconnectedState',
'ConnectionRefusedError', 'ServerNotFoundError',
'PeerClosedError']:
@ -517,12 +518,15 @@ class TestSendOrListen:
msgs = [e.message for e in caplog.records()]
assert "Got AddressInUseError, trying again." in msgs
@pytest.mark.parametrize('has_error, excname', [
(True, 'SocketError'),
(False, 'AddressInUseError')
@pytest.mark.parametrize('has_error, exc_name, exc_msg', [
(True, 'SocketError',
'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,
stubs, caplog, args, has_error, excname):
stubs, caplog, args, has_error, exc_name,
exc_msg):
"""Test the following scenario:
- First call to send_to_running_instance:
@ -552,12 +556,14 @@ class TestSendOrListen:
msgs = [e.message for e in caplog.records()]
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!',
'pre_text: ',
'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):
"""Test an error with the first listen call."""
@ -571,9 +577,11 @@ class TestSendOrListen:
msgs = [e.message for e in caplog.records()]
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!',
'pre_text: ',
'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
from qutebrowser.utils import error
from qutebrowser.misc import ipc
from PyQt5.QtCore import pyqtSlot, QTimer
from PyQt5.QtWidgets import QMessageBox
@ -33,19 +34,36 @@ from PyQt5.QtWidgets import QMessageBox
Args = collections.namedtuple('Args', 'no_err_windows')
def test_no_err_windows(caplog):
"""Test handle_fatal_exc uwith no_err_windows = True."""
class TestError(Exception):
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:
raise ValueError("exception")
except ValueError as e:
raise exc
except Exception as e:
with caplog.atLevel(logging.ERROR):
error.handle_fatal_exc(e, Args(no_err_windows=True), 'title',
pre_text='pre', post_text='post')
msgs = [rec.message for rec in caplog.records()]
assert msgs[0] == 'Handling fatal ValueError with --no-err-windows!'
assert msgs[1] == 'title: title'
assert msgs[2] == 'pre_text: pre'
assert msgs[3] == 'post_text: post'
expected = [
'Handling fatal {} with --no-err-windows!'.format(name),
'title: title',
'pre_text: pre',
'post_text: post',
'exception text: {}'.format(exc_text),
]
assert msgs[-5:] == expected
@pytest.mark.parametrize('pre_text, post_text, expected', [