Display error when creating embedded bot with incorrect config data.

"incorrect" here means rejected by a bot's validate_config() method.
A common scenario for this is validating API keys before the bot is
created. If validate_config() fails, the bot will not be created.
This commit is contained in:
Robert Hönig
2018-02-13 11:47:40 +01:00
committed by Tim Abbott
parent 194b2fd8c0
commit 649e76e932
3 changed files with 39 additions and 4 deletions

View File

@@ -8,6 +8,8 @@ from zerver.lib.request import JsonableError
from zerver.models import UserProfile, Service, Realm, \
get_user_profile_by_id
from zulip_bots.custom_exceptions import ConfigValidationError
def check_full_name(full_name_raw: Text) -> Text:
full_name = full_name_raw.strip()
if len(full_name) > UserProfile.MAX_NAME_LENGTH:
@@ -24,6 +26,20 @@ def check_short_name(short_name_raw: Text) -> Text:
raise JsonableError(_("Bad name or username"))
return short_name
def check_valid_bot_config(service_name: str, config_data: Dict[str, str]) -> None:
try:
from zerver.lib.bot_lib import get_bot_handler
bot_handler = get_bot_handler(service_name)
if hasattr(bot_handler, 'validate_config'):
bot_handler.validate_config(config_data)
except ConfigValidationError:
# The exception provides a specific error message, but that
# message is not tagged translatable, because it is
# triggered in the external zulip_bots package.
# TODO: Think of some clever way to provide a more specific
# error message.
raise JsonableError(_("Invalid configuration data!"))
def check_valid_bot_type(user_profile: UserProfile, bot_type: int) -> None:
if bot_type not in user_profile.allowed_bot_types:
raise JsonableError(_('Invalid bot type'))