Repository: topoteretes/cognee
File: cognee/modules/ingestion/data_types/S3BinaryData.py (Python)

Code before:
                self.metadata = await get_file_metadata(file)

            elapsed = time.perf_counter() - start_time
            file_size = self.metadata.get("file_size") if self.metadata else None

            logger.info(
                "S3 metadata fetched",
                extra={
                    "s3_path": self.s3_path,
                    "file_path": file_path,
                    "file_size_bytes": file_size,
                    "duration_seconds": round(elapsed, 3),
                },
            )

            if elapsed > S3_SLOW_OPERATION_THRESHOLD_SEC:
                logger.warning(
                    "S3 metadata fetch slow",
                    extra={
                        "s3_path": self.s3_path,
                        "duration_seconds": round(elapsed, 2),
                        "threshold_seconds": S3_SLOW_OPERATION_THRESHOLD_SEC,
                    },
                )

        except Exception as error:
            logger.error(
                "S3 metadata fetch failed",
                extra={
                    "s3_path": self.s3_path,
                    "file_path": file_path,
                    "error": str(error),
                },
                exc_info=True,
            )
            raise

        if self.metadata is not None and self.metadata.get("name") is None:
            self.metadata["name"] = self.name or file_path

    @asynccontextmanager
    async def get_data(self):
        from cognee.infrastructure.files.storage.S3FileStorage import S3FileStorage

        file_dir_path = os.path.dirname(self.s3_path)
        file_path = os.path.basename(self.s3_path)

        logger.debug(
            "Opening S3 file for read",
            extra={
                "s3_path": self.s3_path,

Review comment (bug):
This try/except is too broad. Both for the code it contains, and the scope of exceptions it catches. If you are to catch exceptions, they should be specific exceptions from a small number of calls, and you should be able to either do something useful to deal with the corner case or be able to add useful extra debugging information.

Code after:
                    "error": str(error),
                },
                exc_info=True,
            )
            raise

        elapsed = time.perf_counter() - start_time
        duration_sec = round(elapsed, 3)
        if elapsed > S3_SLOW_OPERATION_THRESHOLD_SEC:
            logger.warning(
                "S3 metadata fetch slow",
                extra={
                    "s3_path": self.s3_path,
                    "file_path": file_path,
                    "duration_seconds": duration_sec,
                    "threshold_seconds": S3_SLOW_OPERATION_THRESHOLD_SEC,
                },
            )
        else:
            file_size = self.metadata.get("file_size") if self.metadata else None
            logger.info(
                "S3 metadata fetched",
                extra={
                    "s3_path": self.s3_path,
                    "file_path": file_path,
                    "file_size_bytes": file_size,
                    "duration_seconds": duration_sec,
                },
            )

        if self.metadata is not None and self.metadata.get("name") is None:
            self.metadata["name"] = self.name or file_path

    @asynccontextmanager
    async def get_data(self):
        from cognee.infrastructure.files.storage.S3FileStorage import S3FileStorage

        file_dir_path = os.path.dirname(self.s3_path)
        file_path = os.path.basename(self.s3_path)
        file_storage = S3FileStorage(file_dir_path)
        async with file_storage.open(file_path, "rb") as file:
            yield file