Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Map the 502 and 429 errors
Tomas Valenta74120ea53bab
diff --git a/packages/js-sdk/example.mts b/packages/js-sdk/example.mtsindex 7065683cb..08982521c 100644--- a/packages/js-sdk/example.mts+++ b/packages/js-sdk/example.mts@@ -1,13 +1,13 @@import { Sandbox } from './dist'-const sbx = await Sandbox.create('3pcmtxu6u8it5f6flpkk', {- debug: true,+const sbx = await Sandbox.connect('3pcmtxu6u8it5f6flpkk', {+ // debug: true,// domain: 'e2b-staging.com',// timeoutMs: 5_000,// logger: console,})-const res = await sbx.files.list('/', { user: 'adada' })+const res = await sbx.files.list('/')// console.log(res)// const l = await sbx.commands.list()diff --git a/packages/js-sdk/src/envd/api.ts b/packages/js-sdk/src/envd/api.tsindex e0f47b38e..75e1a86e2 100644--- a/packages/js-sdk/src/envd/api.ts+++ b/packages/js-sdk/src/envd/api.ts@@ -3,7 +3,7 @@ import createClient from 'openapi-fetch'import type { components, paths } from './schema.gen'import { ConnectionConfig } from '../connectionConfig'import { createApiLogger } from '../logs'-import { SandboxError, InvalidUserError, InvalidPathError, NotFoundError, NotEnoughDiskSpaceError, formatSandboxTimeoutError } from '../errors'+import { SandboxError, InvalidArgumentError, NotFoundError, NotEnoughSpaceError, formatSandboxTimeoutError, AuthenticationError } from '../errors'export function handleEnvdApiError(err: {code: number;@@ -11,17 +11,17 @@ export function handleEnvdApiError(err: {} | undefined) {switch (err?.code) {case 400:- return new InvalidUserError(err.message)- case 403:- return new InvalidPathError(err.message)+ return new InvalidArgumentError(err.message)+ case 401:+ return new AuthenticationError(err.message)case 404:return new NotFoundError(err.message)- case 412:- return new InvalidPathError(err.message)+ case 429:+ return new SandboxError(`${err.code}: ${err.message}: The requests are being rate limited.`)case 502:return formatSandboxTimeoutError(err.message)case 507:- return new NotEnoughDiskSpaceError(err.message)+ return new NotEnoughSpaceError(err.message)default:if (err) {return new SandboxError(`${err.code}: ${err.message}`)diff --git a/packages/js-sdk/src/envd/rpc.ts b/packages/js-sdk/src/envd/rpc.tsindex d84d64538..93620bc2d 100644--- a/packages/js-sdk/src/envd/rpc.ts+++ b/packages/js-sdk/src/envd/rpc.ts@@ -1,16 +1,18 @@import { Code, ConnectError } from '@connectrpc/connect'import { defaultUsername } from '../connectionConfig'-import { SandboxError, InvalidUserError, InvalidPathError, TimeoutError, formatSandboxTimeoutError } from '../errors'+import { SandboxError, TimeoutError, formatSandboxTimeoutError, InvalidArgumentError, NotFoundError, AuthenticationError } from '../errors'export function handleRpcError(err: unknown): Error {if (err instanceof ConnectError) {switch (err.code) {case Code.InvalidArgument:- return new InvalidUserError(err.message)+ return new InvalidArgumentError(err.message)+ case Code.Unauthenticated:+ return new AuthenticationError(err.message)case Code.NotFound:- return new InvalidPathError(err.message)+ return new NotFoundError(err.message)case Code.Unavailable:return formatSandboxTimeoutError(err.message)case Code.Canceled:diff --git a/packages/js-sdk/src/errors.ts b/packages/js-sdk/src/errors.tsindex 64be65df0..b44f2d338 100644--- a/packages/js-sdk/src/errors.ts+++ b/packages/js-sdk/src/errors.ts@@ -36,32 +36,22 @@ export class TimeoutError extends SandboxError {}/**- * Thrown when an invalid user is provided.+ * Thrown when an invalid argument is provided.*/-export class InvalidUserError extends SandboxError {+export class InvalidArgumentError extends SandboxError {constructor(message: string) {super(message)- this.name = 'InvalidUserError'- }-}--/**- * Thrown when an invalid path is provided.- */-export class InvalidPathError extends SandboxError {- constructor(message: string) {- super(message)- this.name = 'InvalidPathError'+ this.name = 'InvalidArgumentError'}}/*** Thrown when there is not enough disk space.*/-export class NotEnoughDiskSpaceError extends SandboxError {+export class NotEnoughSpaceError extends SandboxError {constructor(message: string) {super(message)- this.name = 'NotEnoughDiskSpaceError'+ this.name = 'NotEnoughSpaceError'}}diff --git a/packages/js-sdk/src/index.ts b/packages/js-sdk/src/index.tsindex 923c05758..7252690f6 100644--- a/packages/js-sdk/src/index.ts+++ b/packages/js-sdk/src/index.ts@@ -6,11 +6,10 @@ export type { components, paths } from './api'export {AuthenticationError,SandboxError,- InvalidPathError,TimeoutError,NotFoundError,- NotEnoughDiskSpaceError,- InvalidUserError,+ NotEnoughSpaceError,+ InvalidArgumentError,} from './errors'export {ConnectionConfig,diff --git a/packages/python-sdk/connect/client.py b/packages/python-sdk/connect/client.pyindex c86861d63..15bce8b91 100644--- a/packages/python-sdk/connect/client.py+++ b/packages/python-sdk/connect/client.py@@ -1,6 +1,7 @@import gzipimport jsonimport struct+from e2b.exceptions import SandboxException, format_sandbox_timeout_exceptionimport httpcorefrom enum import Flag, Enum@@ -55,11 +56,26 @@ def decode_envelope_header(header):return EnvelopeFlags(flags), data_len-def error_for_response(http_resp):+def error_for_response(http_resp: httpcore.Response):try:error = json.loads(http_resp.content)except (json.decoder.JSONDecodeError, KeyError):- return ConnectException(http_resp.status, http_resp.reason)++ if http_resp.status == 429:+ return ConnectException(+ Code.resource_exhausted,+ f"{http_resp.content.decode()} The requests are being rate limited.",+ )+ elif http_resp.status == 502:+ return ConnectException(+ Code.unavailable,+ http_resp.content.decode(),+ )+ else:+ return ConnectException(+ Code.unknown,+ f"{http_resp.status}: {http_resp.content.decode('utf-8')}",+ )else:return make_error(error)diff --git a/packages/python-sdk/e2b/__init__.py b/packages/python-sdk/e2b/__init__.pyindex 04a949d99..1eacf8649 100644--- a/packages/python-sdk/e2b/__init__.py+++ b/packages/python-sdk/e2b/__init__.py@@ -8,13 +8,12 @@ from .connection_config import ()from .exceptions import (- InvalidPathException,SandboxException,TimeoutException,NotFoundException,- InvalidUserException,- NotEnoughDiskSpaceException,AuthenticationException,+ InvalidArgumentException,+ NotEnoughSpaceException,)from .sandbox.sandbox_api import SandboxInfofrom .sandbox.main import Sandboxdiff --git a/packages/python-sdk/e2b/envd/api.py b/packages/python-sdk/e2b/envd/api.pyindex 735acb1c6..fac6097d1 100644--- a/packages/python-sdk/e2b/envd/api.py+++ b/packages/python-sdk/e2b/envd/api.py@@ -2,10 +2,10 @@ import httpxfrom e2b.exceptions import (SandboxException,- InvalidUserException,- InvalidPathException,NotFoundException,- NotEnoughDiskSpaceException,+ AuthenticationException,+ InvalidArgumentException,+ NotEnoughSpaceException,format_sandbox_timeout_exception,)@@ -16,17 +16,17 @@ ENVD_API_HEALTH_ROUTE = "/health"def handle_envd_api_exception(res: httpx.Response):if res.status_code == 400:- return InvalidUserException(res.json().get("message"))- elif res.status_code == 403:- return InvalidPathException(res.json().get("message"))+ return InvalidArgumentException(res.json().get("message"))+ elif res.status_code == 401:+ return AuthenticationException(res.json().get("message"))elif res.status_code == 404:return NotFoundException(res.json().get("message"))- elif res.status_code == 412:- return InvalidPathException(res.json().get("message"))+ elif res.status_code == 429:+ return SandboxException(f"{res.text}: The requests are being rate limited.")elif res.status_code == 502:- return format_sandbox_timeout_exception(res.json().get("message"))+ return format_sandbox_timeout_exception(res.text)elif res.status_code == 507:- return NotEnoughDiskSpaceException(res.json().get("message"))+ return NotEnoughSpaceException(res.json().get("message"))elif res.status_code >= 400:return SandboxException(f"{res.status_code}: {res.json().get('message')}")return Nonediff --git a/packages/python-sdk/e2b/envd/rpc.py b/packages/python-sdk/e2b/envd/rpc.pyindex 21d4a13a8..4e4f372fa 100644--- a/packages/python-sdk/e2b/envd/rpc.py+++ b/packages/python-sdk/e2b/envd/rpc.py@@ -5,10 +5,11 @@ from connect.client import Code, ConnectExceptionfrom e2b.exceptions import (SandboxException,- InvalidUserException,+ InvalidArgumentException,NotFoundException,TimeoutException,format_sandbox_timeout_exception,+ AuthenticationException,)from e2b.connection_config import Username, default_username@@ -16,7 +17,9 @@ from e2b.connection_config import Username, default_usernamedef handle_rpc_exception(e: Exception):if isinstance(e, ConnectException):if e.status == Code.invalid_argument:- return InvalidUserException(e.message)+ return InvalidArgumentException(e.message)+ elif e.status == Code.unauthenticated:+ return AuthenticationException(e.message)elif e.status == Code.not_found:return NotFoundException(e.message)elif e.status == Code.unavailable:diff --git a/packages/python-sdk/e2b/exceptions.py b/packages/python-sdk/e2b/exceptions.pyindex 8eab4383c..61fb9ab1a 100644--- a/packages/python-sdk/e2b/exceptions.py+++ b/packages/python-sdk/e2b/exceptions.py@@ -27,7 +27,7 @@ class TimeoutException(SandboxException):pass-class NotEnoughDiskSpaceException(SandboxException):+class NotEnoughSpaceException(SandboxException):"""Raised when there is not enough disk space."""@@ -43,17 +43,9 @@ class NotFoundException(SandboxException):pass-class InvalidPathException(SandboxException):+class InvalidArgumentException(SandboxException):"""- Raised when an invalid path is provided.- """-- pass---class InvalidUserException(SandboxException):- """- Raised when an invalid user is provided.+ Raised when an invalid argument is provided."""passdiff --git a/packages/python-sdk/example.py b/packages/python-sdk/example.pyindex 8486ccfac..0e8366c98 100644--- a/packages/python-sdk/example.py+++ b/packages/python-sdk/example.py@@ -1,16 +1,6 @@-class A:- template = 2+from e2b import Sandbox- def __init__(self) -> None:- print(self.template)+sbx = Sandbox.connect("acas")-class B(A):- template = 3-- def __init__(self) -> None:- super().__init__()- print(self.template)---b = B()+sbx.files.list("/")