control: add more friendly error message to Switch

This commit is contained in:
Shiz 2021-08-12 21:57:24 +02:00
parent 2fdec1b697
commit 90ed8f1583
1 changed files with 4 additions and 2 deletions

View File

@ -25,8 +25,10 @@ class Switch(G[T, V], Type[T]):
selector = context.peek(self.default_key) if peek else context.get(self.default_key)
else:
return to_type(default)
if selector not in options and default:
return to_type(default)
if selector not in options:
if default:
return to_type(default)
raise ValueError(f'selector {selector} not any in options {", ".join(str(o) for o in options)}')
return to_type(options[selector])
def parse(self, context: Context, stream: Stream) -> T: