Improve error messages with backend conditionals

This commit is contained in:
Florian Bruhin 2018-06-12 11:10:03 +02:00
parent 2f97a597a4
commit 6faff11243
4 changed files with 25 additions and 6 deletions

View File

@ -80,6 +80,7 @@ Changed
(e.g. `--logfilter '!init,destroy'`) to invert the filter. (e.g. `--logfilter '!init,destroy'`) to invert the filter.
- `:view-source` now has a `--pygments` flag which uses the "old" way of - `:view-source` now has a `--pygments` flag which uses the "old" way of
rendering sources even with QtWebEngine. rendering sources even with QtWebEngine.
- Improved error messages when a setting needs a newer Qt version.
Removed Removed
~~~~~~~ ~~~~~~~

View File

@ -276,7 +276,8 @@ class Config(QObject):
"""Set the given option to the given value.""" """Set the given option to the given value."""
if not isinstance(objects.backend, objects.NoBackend): if not isinstance(objects.backend, objects.NoBackend):
if objects.backend not in opt.backends: if objects.backend not in opt.backends:
raise configexc.BackendError(opt.name, objects.backend) raise configexc.BackendError(opt.name, objects.backend,
opt.raw_backends)
opt.typ.to_py(value) # for validation opt.typ.to_py(value) # for validation

View File

@ -44,9 +44,15 @@ class BackendError(Error):
"""Raised when this setting is unavailable with the current backend.""" """Raised when this setting is unavailable with the current backend."""
def __init__(self, name, backend): def __init__(self, name, backend, raw_backends):
super().__init__("The {} setting is not available with the {} " if raw_backends is None or not raw_backends[backend.name]:
"backend!".format(name, backend.name)) msg = ("The {} setting is not available with the {} backend!"
.format(name, backend.name))
else:
msg = ("The {} setting needs {} with the {} backend!"
.format(name, raw_backends[backend.name], backend.name))
super().__init__(msg)
class NoPatternError(Error): class NoPatternError(Error):

View File

@ -54,12 +54,23 @@ def test_no_autoconfig_error():
assert str(e) == expected assert str(e) == expected
def test_backend_error(): @pytest.mark.parametrize('raw_backends', [
e = configexc.BackendError('foo', usertypes.Backend.QtWebKit) None,
{'QtWebEngine': 'Qt 5.11', 'QtWebKit': False}
])
def test_backend_error(raw_backends):
e = configexc.BackendError('foo', usertypes.Backend.QtWebKit, raw_backends)
expected = "The foo setting is not available with the QtWebKit backend!" expected = "The foo setting is not available with the QtWebKit backend!"
assert str(e) == expected assert str(e) == expected
def test_backend_error_condition():
e = configexc.BackendError('foo', usertypes.Backend.QtWebEngine,
{'QtWebEngine': 'Qt 5.11', 'QtWebKit': True})
expected = "The foo setting needs Qt 5.11 with the QtWebEngine backend!"
assert str(e) == expected
def test_no_pattern_error(): def test_no_pattern_error():
e = configexc.NoPatternError('foo') e = configexc.NoPatternError('foo')
expected = "The foo setting does not support URL patterns!" expected = "The foo setting does not support URL patterns!"