From c3155afc21137eb074f25cc032d86a8cc731e8bb Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Sun, 12 Feb 2017 08:53:50 -0500 Subject: [PATCH] Rethrow KeyError as DoesNotExistError in urlmarks. From @TheCompiler: To expand on this: I think it's fine to use KeyError on a lower level, i.e. with the SqlTable object with a dict-like interface. However, on this higher level, I think it makes sense to re-raise them as more specific exceptions. --- qutebrowser/browser/commands.py | 2 +- qutebrowser/browser/urlmarks.py | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 9d19914f8..e5ab0af21 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1261,7 +1261,7 @@ class CommandDispatcher: url = self._current_url() try: quickmark_manager.delete_by_qurl(url) - except KeyError as e: + except urlmarks.DoesNotExistError as e: raise cmdexc.CommandError(str(e)) else: try: diff --git a/qutebrowser/browser/urlmarks.py b/qutebrowser/browser/urlmarks.py index db5e14153..ca71b3f0d 100644 --- a/qutebrowser/browser/urlmarks.py +++ b/qutebrowser/browser/urlmarks.py @@ -53,6 +53,13 @@ class InvalidUrlError(Error): pass +class DoesNotExistError(Error): + + """Exception emitted when a given URL does not exist.""" + + pass + + class AlreadyExistsError(Error): """Exception emitted when a given URL does already exist.""" @@ -179,12 +186,14 @@ class QuickmarkManager(UrlMarkManager): try: self.delete(urlstr, field='url') except KeyError: - raise KeyError("Quickmark for '{}' not found!".format(urlstr)) + raise DoesNotExistError("Quickmark for '{}' not found!" + .format(urlstr)) def get(self, name): """Get the URL of the quickmark named name as a QUrl.""" if name not in self: - raise KeyError("Quickmark '{}' does not exist!".format(name)) + raise DoesNotExistError("Quickmark '{}' does not exist!" + .format(name)) urlstr = self[name] try: url = urlutils.fuzzy_url(urlstr, do_search=False)