Skip to content

Commit 165581a

Browse files
committed
witai: Propagate exceptions from get_handle.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 3855740 commit 165581a

File tree

1 file changed

+8
-19
lines changed
  • zulip_bots/zulip_bots/bots/witai

1 file changed

+8
-19
lines changed

zulip_bots/zulip_bots/bots/witai/witai.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ def initialize(self, bot_handler: BotHandler) -> None:
2828
handler_location = config.get("handler_location")
2929
if not handler_location:
3030
raise KeyError("No `handler_location` was specified")
31-
handle = get_handle(handler_location)
32-
if handle is None:
33-
raise Exception("Could not get handler from handler_location.")
34-
else:
35-
self.handle = handle
31+
self.handle = get_handle(handler_location)
3632

3733
help_message = config.get("help_message")
3834
if not help_message:
@@ -63,7 +59,7 @@ def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> No
6359
handler_class = WitaiHandler
6460

6561

66-
def get_handle(location: str) -> Optional[Callable[[Dict[str, Any]], Optional[str]]]:
62+
def get_handle(location: str) -> Callable[[Dict[str, Any]], Optional[str]]:
6763
"""Returns a function to be used when generating a response from Wit.ai
6864
bot. This function is the function named `handle` in the module at the
6965
given `location`. For an example of a `handle` function, see `doc.md`.
@@ -78,16 +74,9 @@ def get_handle(location: str) -> Optional[Callable[[Dict[str, Any]], Optional[st
7874
Parameters:
7975
- location: The absolute path to the module to look for `handle` in.
8076
"""
81-
try:
82-
spec = importlib.util.spec_from_file_location("module.name", location)
83-
if spec is None:
84-
return None
85-
handler = importlib.util.module_from_spec(spec)
86-
loader = spec.loader
87-
if not isinstance(loader, importlib.abc.Loader):
88-
return None
89-
loader.exec_module(handler)
90-
return handler.handle
91-
except Exception as e:
92-
print(e)
93-
return None
77+
spec = importlib.util.spec_from_file_location("module.name", location)
78+
if spec is None or spec.loader is None:
79+
raise RuntimeError(f"Could not get handler from {location!r}.")
80+
handler = importlib.util.module_from_spec(spec)
81+
spec.loader.exec_module(handler)
82+
return handler.handle

0 commit comments

Comments
 (0)