From 82459f72aa2d29ec04ddae813ca29027a882761f Mon Sep 17 00:00:00 2001 From: Torantulino Date: Thu, 30 Mar 2023 12:46:36 +0100 Subject: [PATCH] Handles errors in get_command without terminating program. --- AutonomousAI/commands.py | 4 ++-- AutonomousAI/main.py | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/AutonomousAI/commands.py b/AutonomousAI/commands.py index cd74e28d..dabdd992 100644 --- a/AutonomousAI/commands.py +++ b/AutonomousAI/commands.py @@ -17,10 +17,10 @@ def get_command(response): return command_name, arguments except json.decoder.JSONDecodeError: - return "Error: Invalid JSON" + return "Error:", "Invalid JSON" # All other errors, return "Error: + error message" except Exception as e: - return "Error: " + str(e) + return "Error:", str(e) def execute_command(command_name, arguments): try: diff --git a/AutonomousAI/main.py b/AutonomousAI/main.py index ed6512cb..1f4b3f69 100644 --- a/AutonomousAI/main.py +++ b/AutonomousAI/main.py @@ -133,8 +133,12 @@ while True: print_assistant_thoughts(assistant_reply) # Get command name and arguments - command_name, arguments = cmd.get_command(assistant_reply) - + try: + command_name, arguments = cmd.get_command(assistant_reply) + except Exception as e: + print_to_console("Error: \n", Fore.RED, str(e)) + + if not continuous_mode: ### GET USER AUTHORIZATION TO EXECUTE COMMAND ### # Get key press: Prompt the user to press enter to continue or escape to exit @@ -162,8 +166,10 @@ while True: print_to_console("NEXT ACTION: ", Fore.CYAN, f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}") # Exectute command - result = cmd.execute_command(command_name, arguments) - + if command_name.lower() != "error": + result = cmd.execute_command(command_name, arguments) + else: + result ="Error: " + arguments # Check if there's a result from the command append it to the message history if result != None: