Skip to content

Commit 1cde6db

Browse files
merged [draft/keywordcall] with [draft/switch] bookmark
--HG-- branch : draft
2 parents f57c3d0 + 6726d49 commit 1cde6db

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

robottools/library/context/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
# From .method:
2929
'contextmethod']
3030

31+
from moretools import qualname
32+
3133
from robottools.library.session.metaoptions import Meta
3234
from robottools.library.keywords import KeywordsDict
3335

@@ -66,6 +68,9 @@ class ContextHandlerMeta(type):
6668
## return type.__new__(metacls, clsname, bases, clsattrs)
6769

6870
def __init__(cls, clsname, bases, clsattrs):
71+
excname = cls.__name__ + 'Error'
72+
cls.ContextError = type(excname, (RuntimeError, ), {})
73+
6974
try:
7075
names = cls.contexts
7176
except AttributeError:
@@ -87,12 +92,17 @@ def switch_context(self, name):
8792
break
8893
for context in cls.contexts:
8994
if context.name == name:
95+
if switch_func: # Custom switch hook
96+
try:
97+
switch_func(self, name)
98+
except Exception as exc:
99+
raise cls.ContextError(
100+
"Couldn't switch context to %s (%s: %s)"
101+
% (repr(name), qualname(type(exc)), exc))
90102
self.contexts.remove(current)
91103
self.contexts.append(context)
92-
if switch_func: # Custom switch hook
93-
switch_func(self, name)
94104
return
95-
raise ValueError(name)
105+
raise cls.ContextError("Context not found: %s" % repr(name))
96106

97107
keyword_name = switch_context.__name__ = 'switch_' + clsname.lower()
98108
cls.keywords[keyword_name] = switch_context

robottools/library/session/meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __new__(mcs, clsname, bases, clsattrs):
6161
clsattrs['meta'] = meta
6262

6363
excname = meta.upper_identifier_name + 'Error'
64-
clsattrs['SessionError'] = type(excname, (RuntimeError,), {})
64+
clsattrs['SessionError'] = type(excname, (RuntimeError, ), {})
6565

6666
# The handler's dictionary of opened sessions
6767
clsattrs['sessions'] = {}

0 commit comments

Comments
 (0)