From 25c04f7d284005f122b1f51e3964c43a6c35b0c7 Mon Sep 17 00:00:00 2001 From: yanghua Date: Tue, 24 Sep 2024 10:02:57 +0800 Subject: [PATCH] Catch exceptions in fetch method to enhance stability --- tosfs/core.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tosfs/core.py b/tosfs/core.py index d2c23dc..c95d323 100644 --- a/tosfs/core.py +++ b/tosfs/core.py @@ -2107,15 +2107,18 @@ def _fetch_range(self, start: int, end: int) -> bytes: def fetch() -> bytes: with io.BytesIO() as temp_buffer: - for chunk in self.fs.tos_client.get_object( - self.bucket, - self.key, - self.version_id, - range_start=start, - range_end=end, - ): - temp_buffer.write(chunk) - return temp_buffer.getvalue() + try: + for chunk in self.fs.tos_client.get_object( + self.bucket, + self.key, + self.version_id, + range_start=start, + range_end=end, + ): + temp_buffer.write(chunk) + return temp_buffer.getvalue() + except Exception as e: + raise TosClientError(f"{e}", e) from e return retryable_func_executor(fetch, max_retry_num=self.fs.max_retry_num)