diff options
author | Florian Bruhin <me@the-compiler.org> | 2022-06-17 17:27:52 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2022-06-17 17:27:52 +0200 |
commit | c8d6b264ae47a8f5143b9a3d9b69f244d0c75578 (patch) | |
tree | 7df98a7b9fefa95867f9303a8ac2b276f70be757 | |
parent | 9b681292ed560f0cca29f3d6398b0fd21edc4ad6 (diff) | |
download | qutebrowser-c8d6b264ae47a8f5143b9a3d9b69f244d0c75578.tar.gz qutebrowser-c8d6b264ae47a8f5143b9a3d9b69f244d0c75578.zip |
greasemonkey: Add error handling
See #7245
-rw-r--r-- | qutebrowser/browser/greasemonkey.py | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/qutebrowser/browser/greasemonkey.py b/qutebrowser/browser/greasemonkey.py index 9ed2d691e..674ccaad1 100644 --- a/qutebrowser/browser/greasemonkey.py +++ b/qutebrowser/browser/greasemonkey.py @@ -233,6 +233,7 @@ class LoadResults: """The results of loading all Greasemonkey scripts.""" successful: List[GreasemonkeyScript] = dataclasses.field(default_factory=list) + errors: List[Tuple[str, str]] = dataclasses.field(default_factory=list) def successful_str(self) -> str: if not self.successful: @@ -241,6 +242,13 @@ class LoadResults: names = '\n'.join(str(script) for script in sorted(self.successful, key=str)) return f"Loaded Greasemonkey scripts:\n\n{names}" + def error_str(self) -> Optional[str]: + if not self.errors: + return None + + lines = '\n'.join(f"{script}: {error}" for script, error in sorted(self.errors)) + return f"Greasemonkey scripts failed to load:\n\n{lines}" + class GreasemonkeyMatcher: @@ -314,24 +322,26 @@ class GreasemonkeyManager(QObject): self._run_end = [] self._run_idle = [] - scripts = [] + successful = [] + errors = [] for scripts_dir in _scripts_dirs(): scripts_dir = os.path.abspath(scripts_dir) log.greasemonkey.debug("Reading scripts from: {}".format(scripts_dir)) for script_filename in glob.glob(os.path.join(scripts_dir, '*.js')): - if not os.path.isfile(script_filename): - continue script_path = os.path.join(scripts_dir, script_filename) - with open(script_path, encoding='utf-8-sig') as script_file: - script = GreasemonkeyScript.parse(script_file.read(), - script_filename) - assert script.name, script - self.add_script(script, force) - scripts.append(script) + try: + with open(script_path, encoding='utf-8-sig') as script_file: + script = GreasemonkeyScript.parse( + script_file.read(), script_filename) + assert script.name, script + self.add_script(script, force) + successful.append(script) + except OSError as e: + errors.append((os.path.basename(script_filename), str(e))) self.scripts_reloaded.emit() - return LoadResults(successful=scripts) + return LoadResults(successful=successful, errors=errors) def add_script(self, script, force=False): """Add a GreasemonkeyScript to this manager. @@ -477,12 +487,19 @@ def greasemonkey_reload(force: bool = False, quiet: bool = False) -> None: if not quiet: message.info(result.successful_str()) + errors = result.error_str() + if errors is not None: + message.error(errors) + def init(): """Initialize Greasemonkey support.""" global gm_manager gm_manager = GreasemonkeyManager() - gm_manager.load_scripts() + result = gm_manager.load_scripts() + errors = result.error_str() + if errors is not None: + message.error(errors) for scripts_dir in _scripts_dirs(): try: |