Skip to content

Commit 9a744d4

Browse files
Fix errors, issues and misconfigurations (#73)
Add try-except blocks to handle potential exceptions in various functions and methods. * **backend/code_parser.py** - Add try-except block in `CodeParser` class constructor to handle potential exceptions. - Add try-except block in `save_analysis_to_db` method to handle potential exceptions. * **database/models.py** - Fix typo in error message in `verify_component_connections` function. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ProjectZeroDays/Project-Red-Sword/pull/73?shareId=d8aaf88e-078b-46e2-a56c-bbac8c43c17d).
2 parents 5e4b417 + 64c32e3 commit 9a744d4

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

backend/code_parser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def __init__(self, code):
2323
except SyntaxError as e:
2424
logging.error(f"SyntaxError: {e}")
2525
raise
26+
except Exception as e:
27+
logging.error(f"Unexpected error in CodeParser constructor: {e}")
28+
raise
2629

2730
def find_functions(self):
2831
return [node.name for node in ast.walk(self.tree) if isinstance(node, ast.FunctionDef)]
@@ -65,6 +68,9 @@ def verify_database_connection(self):
6568
sample_code = "def example():\n return True"
6669
parser = CodeParser(sample_code)
6770
analysis = parser.analyze_code()
68-
parser.save_analysis_to_db("sample_code.py", "Code Analysis", str(analysis), None)
71+
try:
72+
parser.save_analysis_to_db("sample_code.py", "Code Analysis", str(analysis), None)
73+
except Exception as e:
74+
logging.error(f"Unexpected error in save_analysis_to_db: {e}")
6975
parser.verify_database_connection()
7076
print(analysis)

backend/pipeline_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def autogpt_task(self, task):
2525
max_tokens=150
2626
)
2727
return response.choices[0].text.strip()
28+
except openai.error.AuthenticationError as e:
29+
logging.error(f"API key error during autogpt_task: {e}")
30+
return "API key error"
2831
except Exception as e:
2932
logging.error(f"Error during autogpt_task: {e}")
3033
return ""

database/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def verify_component_connections():
7171

7272
# Check modules components
7373
if not all([AlertsNotifications, APTSimulation]):
74-
raise ValueError("Modules component connection check failed")
74+
raise ValueError("Modules components connection check failed")
7575
logging.info("Modules components connection verified.")
7676

7777
except Exception as e:

0 commit comments

Comments
 (0)