Add error handling for JSON serialization failures

- Wrap json.dumps() in try-catch block to handle JSONEncodeError
- Raise RuntimeError with descriptive message for serialization failures
- Addresses PR review feedback for better fault tolerance

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-06-01 06:58:27 +00:00
parent bcc77cb831
commit c1eb48bd6e

View File

@@ -90,23 +90,26 @@ class SQLiteFlowPersistence(FlowPersistence):
f"state_data must be either a Pydantic BaseModel or dict, got {type(state_data)}"
)
with sqlite3.connect(self.db_path) as conn:
conn.execute(
"""
INSERT INTO flow_states (
flow_uuid,
method_name,
timestamp,
state_json
) VALUES (?, ?, ?, ?)
""",
(
try:
with sqlite3.connect(self.db_path) as conn:
conn.execute(
"""
INSERT INTO flow_states (
flow_uuid,
method_name,
datetime.now(timezone.utc).isoformat(),
json.dumps(state_dict, cls=CrewJSONEncoder),
),
)
timestamp,
state_json
) VALUES (?, ?, ?, ?)
""",
(
flow_uuid,
method_name,
datetime.now(timezone.utc).isoformat(),
json.dumps(state_dict, cls=CrewJSONEncoder),
),
)
except json.JSONEncodeError as e:
raise RuntimeError(f"Failed to serialize flow state: {str(e)}") from e
def load_state(self, flow_uuid: str) -> Optional[Dict[str, Any]]:
"""Load the most recent state for a given flow UUID.