@devries-cameron20 / composio Mirror · read-only / commits / 28bcb190d
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 14:45 UTC

refactor(python): collapse redundant download error handlers, cover both

Review follow-up on the download size cap. `requests.exceptions.RequestException` subclasses `OSError`, so the two handlers added for the write loop were byte-identical and the second already subsumed the first. Collapse them into one `except OSError` and say why in a comment, so the next reader does not re-add the redundant clause. Route partial-file cleanup through `_discard_partial_download`, which suppresses cleanup failures: an `OSError` from `unlink` would otherwise replace the `ResponseTooLargeError` or transport error the caller needs. Cover the two error paths that had no tests: a transport failure mid-stream and a failing write both raise `ErrorDownloadingFile` and leave no partial file behind. Without the handler the write failure escapes as a raw `OSError(28)` — the defect these pin. Claude-Session: https://claude.ai/code/session_01K1hH9PMmd6KPKdkACX553z

jkomyno committed Aug 28, 2026 at 09:54 UTC 28bcb190d991b4dc22b7f8ad0ca31fb94bfa8c87
2 files changed +62 -10
python/composio/core/models/_files.py
+18 -10
@@ -1,5 +1,6 @@
1 from __future__ import annotations
2
3 +import contextlib
4 import functools
5 import hashlib
6 import os
@@ -663,6 +664,17 @@ class FileUploadable(BaseModel):
664 return cls(name=file.name, mimetype=mimetype, s3key=s3meta.key)
665
666
667 +def _discard_partial_download(outfile: Path) -> None:
668 + """Remove a half-written download so it is never mistaken for the file.
669 +
670 + Cleanup failures are swallowed on purpose: the error that triggered the
671 + cleanup is what the caller needs to see, and an ``OSError`` raised from
672 + here would replace it.
673 + """
674 + with contextlib.suppress(OSError):
675 + outfile.unlink(missing_ok=True)
676 +
677 +
678 class FileDownloadable(BaseModel):
679 model_config = ConfigDict(json_schema_extra={"file_downloadable": True})
680
@@ -747,18 +759,14 @@ class FileDownloadable(BaseModel):
759 except ResponseTooLargeError:
760 # Propagates uncaught — callers must see the limit hit — but the
761 # truncated file must not be left behind as if it were the download.
750 - outfile.unlink(missing_ok=True)
762 + _discard_partial_download(outfile)
763 raise
752 - except requests.exceptions.RequestException as e:
753 - outfile.unlink(missing_ok=True)
754 - raise ErrorDownloadingFile(
755 - "Error downloading file: "
756 - f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
757 - ) from e
764 except OSError as e:
759 - # A failing `fd.write` (disk full, permissions) would otherwise
760 - # escape the `ErrorDownloadingFile` contract this method documents.
761 - outfile.unlink(missing_ok=True)
765 + # `requests.exceptions.RequestException` subclasses `OSError`, so a
766 + # mid-stream transport failure and a failing `fd.write`/`mkdir`
767 + # (disk full, permissions) both land here — and both owe the caller
768 + # the `ErrorDownloadingFile` this method documents.
769 + _discard_partial_download(outfile)
770 raise ErrorDownloadingFile(
771 "Error downloading file: "
772 f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
python/tests/test_files.py
+44
@@ -2700,6 +2700,50 @@ class TestDownloadSizeLimit:
2700 assert outfile.exists()
2701 assert outfile.read_bytes() == b"x" * 256 + b"y" * 256
2702
2703 + @patch("composio.core.models._files.safe_get")
2704 + def test_download_wraps_stream_failure_and_removes_partial_file(
2705 + self, mock_get, tmp_path
2706 + ):
2707 + """A transport failure mid-stream keeps the documented error contract."""
2708 +
2709 + def failing_stream(chunk_size=None):
2710 + yield b"x" * 256
2711 + raise requests.exceptions.ConnectionError("connection reset")
2712 +
2713 + mock_response = MagicMock()
2714 + mock_response.status_code = 200
2715 + mock_response.headers = {}
2716 + mock_response.iter_content.side_effect = failing_stream
2717 + mock_response.close = MagicMock()
2718 + mock_get.return_value = mock_response
2719 +
2720 + with pytest.raises(ErrorDownloadingFile):
2721 + self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
2722 +
2723 + assert list(tmp_path.iterdir()) == []
2724 +
2725 + @patch("composio.core.models._files.safe_get")
2726 + def test_download_wraps_write_failure_and_removes_partial_file(
2727 + self, mock_get, tmp_path
2728 + ):
2729 + """A disk failure while writing is an `ErrorDownloadingFile`, not a raw OSError."""
2730 +
2731 + def failing_stream(chunk_size=None):
2732 + yield b"x" * 256
2733 + raise OSError(28, "No space left on device")
2734 +
2735 + mock_response = MagicMock()
2736 + mock_response.status_code = 200
2737 + mock_response.headers = {}
2738 + mock_response.iter_content.side_effect = failing_stream
2739 + mock_response.close = MagicMock()
2740 + mock_get.return_value = mock_response
2741 +
2742 + with pytest.raises(ErrorDownloadingFile):
2743 + self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
2744 +
2745 + assert list(tmp_path.iterdir()) == []
2746 +
2747
2748 class TestRedirectHandling:
2749 """Test redirect handling (redirects should be rejected)."""