improved error messages for inexistent userscripts

fixes #1940
This commit is contained in:
Daniel Karbach 2016-09-29 13:29:39 +02:00
parent 80b5c9127e
commit 8eb12c6cb9
3 changed files with 34 additions and 2 deletions

View File

@ -1122,7 +1122,7 @@ class CommandDispatcher:
try: try:
userscripts.run_async(tab, cmd, *args, win_id=self._win_id, userscripts.run_async(tab, cmd, *args, win_id=self._win_id,
env=env, verbose=verbose) env=env, verbose=verbose)
except userscripts.UnsupportedError as e: except (userscripts.UnsupportedError, userscripts.NotFoundError) as e:
raise cmdexc.CommandError(e) raise cmdexc.CommandError(e)
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')

View File

@ -315,6 +315,27 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner):
return return
class NotFoundError(Exception):
"""Raised when spawning a userscript that doesn't exist.
Attributes:
script_name: name of the userscript as called
paths: path names that were searched for the userscript
message: descriptive message string of the error
"""
def __init__(self, script_name, paths=[]):
self.script_name = script_name
self.paths = paths
self.message = "Userscript '" + script_name + "' not found"
if paths:
self.message += " in userscript directory '" + ("' or '".join(paths)) + "'"
def __str__(self):
return self.message
class UnsupportedError(Exception): class UnsupportedError(Exception):
"""Raised when userscripts aren't supported on this platform.""" """Raised when userscripts aren't supported on this platform."""
@ -375,6 +396,13 @@ def run_async(tab, cmd, *args, win_id, env, verbose=False):
if not os.path.exists(cmd_path): if not os.path.exists(cmd_path):
cmd_path = os.path.join(standarddir.system_data(), cmd_path = os.path.join(standarddir.system_data(),
"userscripts", cmd) "userscripts", cmd)
if not os.path.exists(cmd_path):
raise NotFoundError(cmd, [
os.path.join(standarddir.data(), "userscripts"),
os.path.join(standarddir.system_data(), "userscripts"),
])
elif not os.path.exists(cmd_path):
raise NotFoundError(cmd_path)
log.misc.debug("Userscript to run: {}".format(cmd_path)) log.misc.debug("Userscript to run: {}".format(cmd_path))
runner.finished.connect(commandrunner.deleteLater) runner.finished.connect(commandrunner.deleteLater)

View File

@ -10,7 +10,11 @@ Feature: :spawn
Scenario: Starting a userscript which doesn't exist Scenario: Starting a userscript which doesn't exist
When I run :spawn -u this_does_not_exist When I run :spawn -u this_does_not_exist
Then the error "Error while spawning userscript: The process failed to start." should be shown Then regex "Userscript 'this_does_not_exist' not found in userscript directory '(.*)'( or '(.*)')*" should be logged
Scenario: Starting a userscript with absoloute path which doesn't exist
When I run :spawn -u /this_does_not_exist
Then the error "Userscript '/this_does_not_exist' not found" should be shown
# https://github.com/The-Compiler/qutebrowser/issues/1614 # https://github.com/The-Compiler/qutebrowser/issues/1614
@posix @posix