diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 2c790010c..051f86487 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -136,6 +136,7 @@ Changed * `fonts -> messages.info` - The `qute:settings` page now also shows option descriptions. - `qute:version` and `qutebrowser --version` now show various important paths +- `:spawn`/userscripts now show a nicer error when a script wasn't found Deprecated ~~~~~~~~~~ diff --git a/README.asciidoc b/README.asciidoc index 6eea6948b..fc710ac98 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -166,9 +166,9 @@ Contributors, sorted by the number of commits in descending order: * Nathan Isom * Thorsten Wißmann * Austin Anderson +* Daniel Karbach * Jimmy * Niklas Haas -* Daniel Karbach * Alexey "Averrin" Nabrodov * nanjekyejoannah * avk diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index b6e4a2211..8d1b00ed6 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1126,7 +1126,7 @@ class CommandDispatcher: try: userscripts.run_async(tab, cmd, *args, win_id=self._win_id, env=env, verbose=verbose) - except userscripts.UnsupportedError as e: + except userscripts.Error as e: raise cmdexc.CommandError(e) @cmdutils.register(instance='command-dispatcher', scope='window') diff --git a/qutebrowser/commands/userscripts.py b/qutebrowser/commands/userscripts.py index 4601ed528..de557f940 100644 --- a/qutebrowser/commands/userscripts.py +++ b/qutebrowser/commands/userscripts.py @@ -315,7 +315,34 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner): return -class UnsupportedError(Exception): +class Error(Exception): + + """Base class for userscript exceptions.""" + + +class NotFoundError(Error): + + """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 + """ + + def __init__(self, script_name, paths=None): + super().__init__() + self.script_name = script_name + self.paths = paths + + def __str__(self): + msg = "Userscript '{}' not found".format(self.script_name) + if self.paths: + msg += " in userscript directories {}".format( + ', '.join(repr(path) for path in self.paths)) + return msg + + +class UnsupportedError(Error): """Raised when userscripts aren't supported on this platform.""" @@ -323,12 +350,37 @@ class UnsupportedError(Exception): return "Userscripts are not supported on this platform!" +def _lookup_path(cmd): + """Search userscript directories for given command. + + Raises: + NotFoundError if the command could not be found. + + Args: + cmd: The command to look for. + + Returns: + A path to the userscript. + """ + directories = [ + os.path.join(standarddir.data(), "userscripts"), + os.path.join(standarddir.system_data(), "userscripts"), + ] + for directory in directories: + cmd_path = os.path.join(directory, cmd) + if os.path.exists(cmd_path): + return cmd_path + + raise NotFoundError(cmd, directories) + + def run_async(tab, cmd, *args, win_id, env, verbose=False): """Run a userscript after dumping page html/source. Raises: UnsupportedError if userscripts are not supported on the current platform. + NotFoundError if the command could not be found. Args: tab: The WebKitTab/WebEngineTab to get the source from. @@ -371,10 +423,9 @@ def run_async(tab, cmd, *args, win_id, env, verbose=False): # ~/.local/share/qutebrowser/userscripts (or $XDG_DATA_DIR) if not os.path.isabs(cmd_path): log.misc.debug("{} is no absolute path".format(cmd_path)) - cmd_path = os.path.join(standarddir.data(), "userscripts", cmd) - if not os.path.exists(cmd_path): - cmd_path = os.path.join(standarddir.system_data(), - "userscripts", cmd) + cmd_path = _lookup_path(cmd) + elif not os.path.exists(cmd_path): + raise NotFoundError(cmd_path) log.misc.debug("Userscript to run: {}".format(cmd_path)) runner.finished.connect(commandrunner.deleteLater) diff --git a/tests/end2end/features/spawn.feature b/tests/end2end/features/spawn.feature index 5cdeac029..dc12f4078 100644 --- a/tests/end2end/features/spawn.feature +++ b/tests/end2end/features/spawn.feature @@ -10,7 +10,11 @@ Feature: :spawn Scenario: Starting a userscript which doesn't 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 the error "Userscript 'this_does_not_exist' not found in userscript directories *" should be shown + + 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 @posix