summaryrefslogtreecommitdiff
path: root/qutebrowser/keyinput/modeman.py
diff options
context:
space:
mode:
authorConstantine Theocharis <kontheocharis@gmail.com>2020-06-09 13:29:03 +0100
committerConstantine Theocharis <kontheocharis@gmail.com>2020-06-09 13:46:11 +0100
commit2bc14ddc681f946535f905d07d8e4f80013590a5 (patch)
treeee57158a0928b653c26c06ab54b2e6c893bfee04 /qutebrowser/keyinput/modeman.py
parentd4a7d8ef6210002e6990a9108e49ba9b5b134e82 (diff)
downloadqutebrowser-2bc14ddc681f946535f905d07d8e4f80013590a5.tar.gz
qutebrowser-2bc14ddc681f946535f905d07d8e4f80013590a5.zip
Modify modeman.instance() to return specific error when unavailable.
This avoids the need to have a generic 'except KeyError' in statusbar/bar.py when querying the current mode, in the case when the mode manager has not been initialized yet.
Diffstat (limited to 'qutebrowser/keyinput/modeman.py')
-rw-r--r--qutebrowser/keyinput/modeman.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py
index 880b1ec93..eb96020f3 100644
--- a/qutebrowser/keyinput/modeman.py
+++ b/qutebrowser/keyinput/modeman.py
@@ -68,6 +68,14 @@ class NotInModeError(Exception):
"""Exception raised when we want to leave a mode we're not in."""
+class UnavailableError(Exception):
+
+ """Exception raised when trying to access modeman before initialization.
+
+ Thrown by instance() if modeman has not been initialized yet.
+ """
+
+
def init(win_id: int, parent: QObject) -> 'ModeManager':
"""Initialize the mode manager and the keyparsers for the given win_id."""
modeman = ModeManager(win_id, parent)
@@ -169,8 +177,16 @@ def init(win_id: int, parent: QObject) -> 'ModeManager':
def instance(win_id: Union[int, str]) -> 'ModeManager':
- """Get a modemanager object."""
- return objreg.get('mode-manager', scope='window', window=win_id)
+ """Get a modemanager object.
+
+ Raises UnavailableError if there is no instance available yet.
+ """
+ mode_manager = objreg.get('mode-manager', scope='window', window=win_id,
+ default=None)
+ if mode_manager is not None:
+ return mode_manager
+ else:
+ raise UnavailableError("ModeManager is not initialized yet.")
def enter(win_id: int,