botserver: Raise exceptions rather than returning them.

This commit is contained in:
neiljp (Neil Pilgrim) 2019-11-06 08:31:58 -08:00 committed by Tim Abbott
parent fca2404b46
commit 520a004b85

View file

@ -140,7 +140,7 @@ bots_config = {} # type: Dict[str, Dict[str, str]]
@app.route('/', methods=['POST'])
def handle_bot() -> Union[str, BadRequest, Unauthorized]:
def handle_bot() -> str:
event = request.get_json(force=True)
for bot_name, config in bots_config.items():
if config['email'] == event['bot_email']:
@ -148,13 +148,13 @@ def handle_bot() -> Union[str, BadRequest, Unauthorized]:
bot_config = config
break
else:
return BadRequest("Cannot find a bot with email {} in the Botserver "
"configuration file. Do the emails in your botserverrc "
"match the bot emails on the server?".format(event['bot_email']))
raise BadRequest("Cannot find a bot with email {} in the Botserver "
"configuration file. Do the emails in your botserverrc "
"match the bot emails on the server?".format(event['bot_email']))
if bot_config['token'] != event['token']:
return Unauthorized("Request token does not match token found for bot {} in the "
"Botserver configuration file. Do the outgoing webhooks in "
"Zulip point to the right Botserver?".format(event['bot_email']))
raise Unauthorized("Request token does not match token found for bot {} in the "
"Botserver configuration file. Do the outgoing webhooks in "
"Zulip point to the right Botserver?".format(event['bot_email']))
lib_module = app.config.get("BOTS_LIB_MODULES", {})[bot]
bot_handler = app.config.get("BOT_HANDLERS", {})[bot]
message_handler = app.config.get("MESSAGE_HANDLERS", {})[bot]