From 5e2c38c34933aba3cfd91106a58b26d13d98545c Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:26:37 +0000 Subject: [PATCH] Improve FileReadTool error handling and validation Co-Authored-By: Joe Moura --- .../tools/file_read_tool/file_read_tool.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/crewai_tools/tools/file_read_tool/file_read_tool.py b/src/crewai_tools/tools/file_read_tool/file_read_tool.py index 8a6c2e2d8..32db13f21 100644 --- a/src/crewai_tools/tools/file_read_tool/file_read_tool.py +++ b/src/crewai_tools/tools/file_read_tool/file_read_tool.py @@ -4,13 +4,7 @@ from crewai.tools import BaseTool from pydantic import BaseModel, Field -class FixedFileReadToolSchema(BaseModel): - """Input for FileReadTool.""" - - pass - - -class FileReadToolSchema(FixedFileReadToolSchema): +class FileReadToolSchema(BaseModel): """Input for FileReadTool.""" file_path: str = Field(..., description="Mandatory file full path to read the file") @@ -33,9 +27,16 @@ class FileReadTool(BaseTool): self, **kwargs: Any, ) -> Any: + file_path = kwargs.get("file_path", self.file_path) + if file_path is None: + return "Error: No file path provided. Please provide a file path either in the constructor or as an argument." + try: - file_path = kwargs.get("file_path", self.file_path) with open(file_path, "r") as file: return file.read() + except FileNotFoundError: + return f"Error: File not found at path: {file_path}" + except PermissionError: + return f"Error: Permission denied when trying to read file: {file_path}" except Exception as e: - return f"Fail to read the file {file_path}. Error: {e}" + return f"Error: Failed to read file {file_path}. {str(e)}"