From 57c90ddca031c819569931d4d9f0625bcaba2056 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 27 Nov 2017 14:47:59 -0800 Subject: [PATCH] refactor: Extract display_config_file_errors(). This will be useful for handling errors with third party config files. --- zulip_bots/zulip_bots/lib.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index 0d45c54..1a5da6e 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -200,6 +200,12 @@ def is_private_message_from_another_user(message_dict, current_user_id): return current_user_id != message_dict['sender_id'] return False +def display_config_file_errors(error_msg, config_file): + # type: (str, str) -> None + file_contents = open(config_file).read() + print('\nERROR: {} seems to be broken:\n\n{}'.format(config_file, file_contents)) + print('\nMore details here:\n\n{}\n'.format(error_msg)) + def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name): # type: (Any, bool, str, str) -> Any # @@ -221,9 +227,7 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name): try: client = Client(config_file=config_file, client=client_name) except configparser.Error as e: - file_contents = open(config_file).read() - print('\nERROR: {} seems to be broken:\n\n{}'.format(config_file, file_contents)) - print('\nMore details here:\n\n' + str(e) + '\n') + display_config_file_errors(str(e), config_file) sys.exit(1) bot_dir = os.path.dirname(lib_module.__file__)