Handle missing config files gracefully.

Before this commit, you would get a traceback if you supplied
a non-existent filename for your config file.  Now we exit
gracefully with a useful error message.
This commit is contained in:
Steve Howell 2017-11-20 14:58:20 -08:00
parent f6440e5b65
commit 73ebd719b2
2 changed files with 20 additions and 2 deletions

View file

@ -75,6 +75,18 @@ def parse_args():
return args return args
def exit_gracefully_if_config_file_does_not_exist(config_file):
# type: (str) -> None
if not os.path.exists(config_file):
print('''
ERROR: %s does not exist.
You may need to download a config file from the Zulip app, or
if you have already done that, you need to specify the file
location correctly.
''' % (config_file,))
sys.exit(1)
def main(): def main():
# type: () -> None # type: () -> None
args = parse_args() args = parse_args()
@ -91,6 +103,8 @@ def main():
if not args.quiet: if not args.quiet:
logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.basicConfig(stream=sys.stdout, level=logging.INFO)
exit_gracefully_if_config_file_does_not_exist(args.config_file)
run_message_handler_for_bot( run_message_handler_for_bot(
lib_module=lib_module, lib_module=lib_module,
config_file=args.config_file, config_file=args.config_file,

View file

@ -29,7 +29,9 @@ class TestDefaultArguments(TestCase):
@patch('zulip_bots.run.run_message_handler_for_bot') @patch('zulip_bots.run.run_message_handler_for_bot')
def test_argument_parsing_with_bot_name(self, mock_run_message_handler_for_bot): def test_argument_parsing_with_bot_name(self, mock_run_message_handler_for_bot):
# type: (mock.Mock) -> None # type: (mock.Mock) -> None
with patch('zulip_bots.run.exit_gracefully_if_config_file_does_not_exist'):
zulip_bots.run.main() zulip_bots.run.main()
mock_run_message_handler_for_bot.assert_called_with(bot_name='giphy', mock_run_message_handler_for_bot.assert_called_with(bot_name='giphy',
config_file='/foo/bar/baz.conf', config_file='/foo/bar/baz.conf',
lib_module=mock.ANY, lib_module=mock.ANY,
@ -39,7 +41,9 @@ class TestDefaultArguments(TestCase):
@patch('zulip_bots.run.run_message_handler_for_bot') @patch('zulip_bots.run.run_message_handler_for_bot')
def test_argument_parsing_with_bot_path(self, mock_run_message_handler_for_bot): def test_argument_parsing_with_bot_path(self, mock_run_message_handler_for_bot):
# type: (mock.Mock) -> None # type: (mock.Mock) -> None
with patch('zulip_bots.run.exit_gracefully_if_config_file_does_not_exist'):
zulip_bots.run.main() zulip_bots.run.main()
mock_run_message_handler_for_bot.assert_called_with( mock_run_message_handler_for_bot.assert_called_with(
bot_name='giphy', bot_name='giphy',
config_file='/foo/bar/baz.conf', config_file='/foo/bar/baz.conf',