Skip to content

armgit / E2B

Public Imported from GitHub
0

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

fix(sdk): consistent rate limit error for envd 429 responses (#1387)

Mish Ushakov5b2bb941de9f
diff --git a/.changeset/rate-limit-error-consistency.md b/.changeset/rate-limit-error-consistency.md
new file mode 100644
index 000000000..dc18c9652
--- /dev/null
+++ b/.changeset/rate-limit-error-consistency.md
@@ -0,0 +1,6 @@
+---
+"e2b": patch
+"@e2b/python-sdk": patch
+---
+
+Return a dedicated rate limit error for HTTP 429 responses from the envd API. Previously these were surfaced as a generic sandbox error, unlike the main API client which already raised `RateLimitError` (JS) / `RateLimitException` (Python). Rate limit errors are now consistent across all SDK request paths.
diff --git a/packages/js-sdk/src/envd/api.ts b/packages/js-sdk/src/envd/api.ts
index b0bd1704c..0e5cd467d 100644
--- a/packages/js-sdk/src/envd/api.ts
+++ b/packages/js-sdk/src/envd/api.ts
@@ -11,6 +11,7 @@ import {
SandboxNotFoundError,
formatSandboxTimeoutError,
AuthenticationError,
+ RateLimitError,
} from '../errors'
import { StartResponse, ConnectResponse } from './process/process_pb'
import { Code, ConnectError } from '@connectrpc/connect'
@@ -23,7 +24,7 @@ const DEFAULT_ERROR_MAP: Record<number, (message: string) => Error> = {
401: (message) => new AuthenticationError(message),
404: (message) => new NotFoundError(message),
429: (message) =>
- new SandboxError(`${message}: The requests are being rate limited.`),
+ new RateLimitError(`${message}: The requests are being rate limited.`),
502: formatSandboxTimeoutError,
507: (message) => new NotEnoughSpaceError(message),
}
diff --git a/packages/js-sdk/src/envd/rpc.ts b/packages/js-sdk/src/envd/rpc.ts
index 10d64282b..1b559c72e 100644
--- a/packages/js-sdk/src/envd/rpc.ts
+++ b/packages/js-sdk/src/envd/rpc.ts
@@ -8,6 +8,7 @@ import {
formatSandboxTimeoutError,
InvalidArgumentError,
NotFoundError,
+ RateLimitError,
SandboxError,
TimeoutError,
} from '../errors'
@@ -17,6 +18,10 @@ const DEFAULT_ERROR_MAP: Partial<Record<Code, (message: string) => Error>> = {
[Code.InvalidArgument]: (message) => new InvalidArgumentError(message),
[Code.Unauthenticated]: (message) => new AuthenticationError(message),
[Code.NotFound]: (message) => new NotFoundError(message),
+ [Code.ResourceExhausted]: (message) =>
+ new RateLimitError(
+ `${message}: Rate limit exceeded, please try again later.`
+ ),
[Code.Unavailable]: formatSandboxTimeoutError,
[Code.Canceled]: (message) =>
new TimeoutError(
diff --git a/packages/js-sdk/tests/envd/handleEnvdApiError.test.ts b/packages/js-sdk/tests/envd/handleEnvdApiError.test.ts
new file mode 100644
index 000000000..55f32e734
--- /dev/null
+++ b/packages/js-sdk/tests/envd/handleEnvdApiError.test.ts
@@ -0,0 +1,80 @@
+import { assert, test, describe } from 'vitest'
+import { handleEnvdApiError } from '../../src/envd/api'
+import {
+ AuthenticationError,
+ InvalidArgumentError,
+ NotEnoughSpaceError,
+ NotFoundError,
+ RateLimitError,
+ SandboxError,
+ TimeoutError,
+} from '../../src/errors'
+
+function createMockResponse(
+ status: number,
+ error: { message?: string } | string
+): {
+ error: { message?: string } | string
+ response: Response
+} {
+ return {
+ error,
+ response: {
+ status,
+ text: async () => (typeof error === 'string' ? error : ''),
+ } as Response,
+ }
+}
+
+describe('handleEnvdApiError', () => {
+ test('returns undefined for a successful response', async () => {
+ const err = await handleEnvdApiError({
+ response: { status: 200 } as Response,
+ })
+ assert.isUndefined(err)
+ })
+
+ test('returns InvalidArgumentError for 400', async () => {
+ const res = createMockResponse(400, { message: 'Bad request' })
+ const err = await handleEnvdApiError(res)
+ assert.instanceOf(err, InvalidArgumentError)
+ })
+
+ test('returns AuthenticationError for 401', async () => {
+ const res = createMockResponse(401, { message: 'Invalid token' })
+ const err = await handleEnvdApiError(res)
+ assert.instanceOf(err, AuthenticationError)
+ })
+
+ test('returns NotFoundError for 404', async () => {
+ const res = createMockResponse(404, { message: 'Not found' })
+ const err = await handleEnvdApiError(res)
+ assert.instanceOf(err, NotFoundError)
+ })
+
+ test('returns RateLimitError for 429', async () => {
+ const res = createMockResponse(429, { message: 'Too many requests' })
+ const err = await handleEnvdApiError(res)
+ assert.instanceOf(err, RateLimitError)
+ assert.include(err?.message, 'rate limited')
+ })
+
+ test('returns TimeoutError for 502', async () => {
+ const res = createMockResponse(502, { message: 'Bad gateway' })
+ const err = await handleEnvdApiError(res)
+ assert.instanceOf(err, TimeoutError)
+ })
+
+ test('returns NotEnoughSpaceError for 507', async () => {
+ const res = createMockResponse(507, { message: 'No space left' })
+ const err = await handleEnvdApiError(res)
+ assert.instanceOf(err, NotEnoughSpaceError)
+ })
+
+ test('falls back to SandboxError for unmapped status', async () => {
+ const res = createMockResponse(500, { message: 'Internal error' })
+ const err = await handleEnvdApiError(res)
+ assert.instanceOf(err, SandboxError)
+ assert.include(err?.message, '500')
+ })
+})
diff --git a/packages/js-sdk/tests/envd/handleRpcError.test.ts b/packages/js-sdk/tests/envd/handleRpcError.test.ts
new file mode 100644
index 000000000..590ae6a72
--- /dev/null
+++ b/packages/js-sdk/tests/envd/handleRpcError.test.ts
@@ -0,0 +1,52 @@
+import { assert, test, describe } from 'vitest'
+import { Code, ConnectError } from '@connectrpc/connect'
+import { handleRpcError } from '../../src/envd/rpc'
+import {
+ AuthenticationError,
+ InvalidArgumentError,
+ NotFoundError,
+ RateLimitError,
+ SandboxError,
+ TimeoutError,
+} from '../../src/errors'
+
+describe('handleRpcError', () => {
+ test('returns InvalidArgumentError for InvalidArgument', () => {
+ const err = handleRpcError(new ConnectError('bad', Code.InvalidArgument))
+ assert.instanceOf(err, InvalidArgumentError)
+ })
+
+ test('returns AuthenticationError for Unauthenticated', () => {
+ const err = handleRpcError(new ConnectError('nope', Code.Unauthenticated))
+ assert.instanceOf(err, AuthenticationError)
+ })
+
+ test('returns NotFoundError for NotFound', () => {
+ const err = handleRpcError(new ConnectError('missing', Code.NotFound))
+ assert.instanceOf(err, NotFoundError)
+ })
+
+ test('returns RateLimitError for ResourceExhausted', () => {
+ const err = handleRpcError(
+ new ConnectError('too many', Code.ResourceExhausted)
+ )
+ assert.instanceOf(err, RateLimitError)
+ assert.include(err.message, 'Rate limit')
+ })
+
+ test('returns TimeoutError for Unavailable', () => {
+ const err = handleRpcError(new ConnectError('gone', Code.Unavailable))
+ assert.instanceOf(err, TimeoutError)
+ })
+
+ test('falls back to SandboxError for unmapped code', () => {
+ const err = handleRpcError(new ConnectError('boom', Code.Internal))
+ assert.instanceOf(err, SandboxError)
+ })
+
+ test('returns the original error when not a ConnectError', () => {
+ const original = new Error('not connect')
+ const err = handleRpcError(original)
+ assert.strictEqual(err, original)
+ })
+})
diff --git a/packages/python-sdk/e2b/connection_config.py b/packages/python-sdk/e2b/connection_config.py
index 4189ccf31..1b914093b 100644
--- a/packages/python-sdk/e2b/connection_config.py
+++ b/packages/python-sdk/e2b/connection_config.py
@@ -105,13 +105,6 @@ class ConnectionConfig:
request_timeout,
)
- if request_timeout == 0:
- self.request_timeout = None
- elif request_timeout is not None:
- self.request_timeout = request_timeout
- else:
- self.request_timeout = REQUEST_TIMEOUT
-
self.api_url = (
api_url
or ConnectionConfig._api_url()
diff --git a/packages/python-sdk/e2b/envd/api.py b/packages/python-sdk/e2b/envd/api.py
index 7e2a59c13..7074f9e6a 100644
--- a/packages/python-sdk/e2b/envd/api.py
+++ b/packages/python-sdk/e2b/envd/api.py
@@ -9,6 +9,7 @@ from e2b.exceptions import (
AuthenticationException,
InvalidArgumentException,
NotEnoughSpaceException,
+ RateLimitException,
format_sandbox_timeout_exception,
)
@@ -20,7 +21,7 @@ _DEFAULT_API_ERROR_MAP: dict[int, Callable[[str], Exception]] = {
400: InvalidArgumentException,
401: AuthenticationException,
404: NotFoundException,
- 429: lambda message: SandboxException(
+ 429: lambda message: RateLimitException(
f"{message}: The requests are being rate limited."
),
502: format_sandbox_timeout_exception,
diff --git a/packages/python-sdk/tests/test_envd_api_exception.py b/packages/python-sdk/tests/test_envd_api_exception.py
new file mode 100644
index 000000000..5c501391c
--- /dev/null
+++ b/packages/python-sdk/tests/test_envd_api_exception.py
@@ -0,0 +1,47 @@
+from e2b.envd.api import format_envd_api_exception
+from e2b.exceptions import (
+ AuthenticationException,
+ InvalidArgumentException,
+ NotEnoughSpaceException,
+ NotFoundException,
+ RateLimitException,
+ SandboxException,
+ TimeoutException,
+)
+
+
+def test_maps_400_to_invalid_argument():
+ err = format_envd_api_exception(400, "Bad request")
+ assert isinstance(err, InvalidArgumentException)
+
+
+def test_maps_401_to_authentication():
+ err = format_envd_api_exception(401, "Invalid token")
+ assert isinstance(err, AuthenticationException)
+
+
+def test_maps_404_to_not_found():
+ err = format_envd_api_exception(404, "Not found")
+ assert isinstance(err, NotFoundException)
+
+
+def test_maps_429_to_rate_limit():
+ err = format_envd_api_exception(429, "Too many requests")
+ assert isinstance(err, RateLimitException)
+ assert "rate limited" in str(err)
+
+
+def test_maps_502_to_timeout():
+ err = format_envd_api_exception(502, "Bad gateway")
+ assert isinstance(err, TimeoutException)
+
+
+def test_maps_507_to_not_enough_space():
+ err = format_envd_api_exception(507, "No space left")
+ assert isinstance(err, NotEnoughSpaceException)
+
+
+def test_falls_back_to_sandbox_exception():
+ err = format_envd_api_exception(500, "Internal error")
+ assert isinstance(err, SandboxException)
+ assert "500" in str(err)
diff --git a/packages/python-sdk/tests/test_envd_rpc_exception.py b/packages/python-sdk/tests/test_envd_rpc_exception.py
new file mode 100644
index 000000000..42a455ee2
--- /dev/null
+++ b/packages/python-sdk/tests/test_envd_rpc_exception.py
@@ -0,0 +1,47 @@
+from e2b_connect.client import Code, ConnectException
+from e2b.envd.rpc import handle_rpc_exception
+from e2b.exceptions import (
+ AuthenticationException,
+ InvalidArgumentException,
+ NotFoundException,
+ RateLimitException,
+ SandboxException,
+ TimeoutException,
+)
+
+
+def test_maps_invalid_argument():
+ err = handle_rpc_exception(ConnectException(Code.invalid_argument, "bad"))
+ assert isinstance(err, InvalidArgumentException)
+
+
+def test_maps_unauthenticated():
+ err = handle_rpc_exception(ConnectException(Code.unauthenticated, "nope"))
+ assert isinstance(err, AuthenticationException)
+
+
+def test_maps_not_found():
+ err = handle_rpc_exception(ConnectException(Code.not_found, "missing"))
+ assert isinstance(err, NotFoundException)
+
+
+def test_maps_resource_exhausted_to_rate_limit():
+ err = handle_rpc_exception(ConnectException(Code.resource_exhausted, "too many"))
+ assert isinstance(err, RateLimitException)
+ assert "Rate limit" in str(err)
+
+
+def test_maps_unavailable_to_timeout():
+ err = handle_rpc_exception(ConnectException(Code.unavailable, "gone"))
+ assert isinstance(err, TimeoutException)
+
+
+def test_falls_back_to_sandbox_exception():
+ err = handle_rpc_exception(ConnectException(Code.internal, "boom"))
+ assert isinstance(err, SandboxException)
+
+
+def test_returns_original_when_not_connect_exception():
+ original = ValueError("not connect")
+ err = handle_rpc_exception(original)
+ assert err is original