botserver: Make bot-name-not-found error more user-friendly.

Previously, when a bot name wasn't found, the Botserver threw
an ImportError with an error message. This results in an
intimidating traceback which is in most cases not helpful to
the user. This commit replaces the ImportError with sys.exit.
This commit is contained in:
Robert Hönig 2018-06-01 14:26:20 +02:00 committed by Tim Abbott
parent c85b42be9f
commit 3ddc8f9b5d
2 changed files with 15 additions and 16 deletions

View file

@ -3,7 +3,6 @@ import os
from typing import Any, Dict from typing import Any, Dict
import unittest import unittest
from .server_test_lib import BotServerTestCase from .server_test_lib import BotServerTestCase
import six
import json import json
from zulip_botserver import server from zulip_botserver import server
@ -108,19 +107,19 @@ class BotServerTests(BotServerTestCase):
'token': 'abcd1234', 'token': 'abcd1234',
} }
} }
# TODO: The following passes mypy, though the six stubs don't match the # This works, but mypy still complains:
# unittest ones, so we could file a mypy bug to improve this. # error: No overload variant of "assertRaisesRegexp" of "TestCase" matches argument types
six.assertRaisesRegex(self, # [def (*args: builtins.object, **kwargs: builtins.object) -> builtins.SystemExit, builtins.str]
ImportError, with self.assertRaisesRegexp(SystemExit, # type: ignore
"Bot \"nonexistent-bot\" doesn't exists. Please " 'Error: Bot "nonexistent-bot" doesn\'t exist. Please make '
"make sure you have set up the botserverrc file correctly.", 'sure you have set up the botserverrc file correctly.'):
lambda: self.assert_bot_server_response( self.assert_bot_server_response(
available_bots=available_bots, available_bots=available_bots,
event=dict(message={'content': "@**test** test message"}, event=dict(message={'content': "@**test** test message"},
bot_email='helloworld-bot@zulip.com', bot_email='helloworld-bot@zulip.com',
trigger='mention', trigger='mention',
token='abcd1234'), token='abcd1234'),
bots_config=bots_config)) bots_config=bots_config)
@mock.patch('sys.argv', ['zulip-botserver', '--config-file', '/foo/bar/baz.conf']) @mock.patch('sys.argv', ['zulip-botserver', '--config-file', '/foo/bar/baz.conf'])
def test_argument_parsing_defaults(self) -> None: def test_argument_parsing_defaults(self) -> None:

View file

@ -57,8 +57,8 @@ def load_lib_modules(available_bots: List[str]) -> Dict[str, Any]:
lib_module = import_module(module_name) lib_module = import_module(module_name)
bots_lib_module[bot] = lib_module bots_lib_module[bot] = lib_module
except ImportError: except ImportError:
raise ImportError( sys.exit(
"\nImport Error: Bot \"{}\" doesn't exists. " "Error: Bot \"{}\" doesn't exist. "
"Please make sure you have set up the botserverrc file correctly.\n".format(bot) "Please make sure you have set up the botserverrc file correctly.\n".format(bot)
) )
return bots_lib_module return bots_lib_module