Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix exists error handling and return type

Tomas Valenta4e60676365c0
diff --git a/packages/js-sdk/src/sandbox/filesystem/index.ts b/packages/js-sdk/src/sandbox/filesystem/index.ts
index 9aec60c1d..77cb5baca 100644
--- a/packages/js-sdk/src/sandbox/filesystem/index.ts
+++ b/packages/js-sdk/src/sandbox/filesystem/index.ts
@@ -3,6 +3,8 @@ import {
createPromiseClient,
Transport,
PromiseClient,
+ ConnectError,
+ Code,
} from '@connectrpc/connect'
import { ConnectionOpts, defaultUsername, Username } from '../../connectionConfig'
@@ -112,19 +114,30 @@ export class Filesystem {
})
}
- async exists(path: string, opts?: FilesystemRequestOpts): Promise<EntryInfo> {
- const res = await this.rpc.stat({
- path,
- user: {
- selector: {
- case: 'username',
- value: opts?.user || defaultUsername,
+ async exists(path: string, opts?: FilesystemRequestOpts): Promise<boolean> {
+ try {
+ await this.rpc.stat({
+ path,
+ user: {
+ selector: {
+ case: 'username',
+ value: opts?.user || defaultUsername,
+ },
},
- },
- }, {
- timeoutMs: opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMs,
- })
- return res.entry!
+ }, {
+ timeoutMs: opts?.requestTimeoutMs ?? this.connectionConfig.requestTimeoutMs,
+ })
+
+ return true
+ } catch (err) {
+ const connectErr = ConnectError.from(err)
+
+ if (connectErr.code === Code.NotFound) {
+ return false
+ }
+
+ throw err
+ }
}
async watch(
diff --git a/packages/python-sdk/connect/__init__.py b/packages/python-sdk/connect/__init__.py
index 00f5834f3..d29e6ac01 100644
--- a/packages/python-sdk/connect/__init__.py
+++ b/packages/python-sdk/connect/__init__.py
@@ -1 +1 @@
-from .client import Client, Code, GzipCompressor # noqa: F401
+from .client import Client, Code, GzipCompressor, Error # noqa: F401
diff --git a/packages/python-sdk/connect/client.py b/packages/python-sdk/connect/client.py
index 1940e2a50..803f64229 100644
--- a/packages/python-sdk/connect/client.py
+++ b/packages/python-sdk/connect/client.py
@@ -33,7 +33,7 @@ class Code(IntEnum):
class Error(Exception):
- def __init__(self, code, message):
+ def __init__(self, code: Code, message: str):
self.code = code
self.message = message
@@ -118,7 +118,14 @@ class ProtobufCodec:
class Client:
def __init__(
- self, *, pool, url, response_type, compressor=None, json=False, headers=None
+ self,
+ *,
+ pool,
+ url,
+ response_type,
+ compressor=None,
+ json=False,
+ headers=None,
):
if pool is None:
pool = default_connection_pool()
diff --git a/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py
index 673665aec..e0c8b9378 100644
--- a/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox/filesystem/filesystem.py
@@ -103,11 +103,17 @@ class Filesystem:
)
return [entry for entry in res.entries]
- def exists(self, path: str) -> filesystem_pb2.EntryInfo:
- res = self._rpc.stat(
- filesystem_pb2.StatRequest(path=path),
- )
- return res.entry
+ def exists(self, path: str) -> bool:
+ try:
+ self._rpc.stat(
+ filesystem_pb2.StatRequest(path=path),
+ )
+ return True
+
+ except connect.Error as e:
+ if e.code == connect.Code.not_found:
+ return False
+ raise
def remove(self, path: str) -> None:
self._rpc.remove(