Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add rate limit error
Jakub Novak80f2159cc73d
diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.tsindex ad49d2b90..0e62b3778 100644--- a/packages/js-sdk/src/api/index.ts+++ b/packages/js-sdk/src/api/index.ts@@ -3,7 +3,7 @@ import createClient, { FetchResponse } from 'openapi-fetch'import type { components, paths } from './schema.gen'import { defaultHeaders } from './metadata'import { ConnectionConfig } from '../connectionConfig'-import { AuthenticationError, SandboxError } from '../errors'+import { AuthenticationError, RateLimitError, SandboxError } from '../errors'import { createApiLogger } from '../logs'export function handleApiError(@@ -13,6 +13,12 @@ export function handleApiError(return}+ if (response.response.status === 429) {+ return new RateLimitError(+ 'Authentication failed, please check your API key and access token.'+ )+ }+const message = response.error?.message ?? response.errorreturn new SandboxError(`${response.response.status}: ${message}`)}diff --git a/packages/js-sdk/src/errors.ts b/packages/js-sdk/src/errors.tsindex e571bed40..271b68133 100644--- a/packages/js-sdk/src/errors.ts+++ b/packages/js-sdk/src/errors.ts@@ -7,7 +7,7 @@ export function formatSandboxTimeoutError(message: string) {/*** Base class for all sandbox errors.- *+ ** Thrown when general sandbox errors occur.*/export class SandboxError extends Error {@@ -19,13 +19,13 @@ export class SandboxError extends Error {/*** Thrown when a timeout error occurs.- *+ ** The [unavailable] error type is caused by sandbox timeout.- *+ ** The [canceled] error type is caused by exceeding request timeout.- *+ ** The [deadline_exceeded] error type is caused by exceeding the timeout for command execution, watch, etc.- *+ ** The [unknown] error type is sometimes caused by the sandbox timeout when the request is not processed correctly.*/export class TimeoutError extends SandboxError {@@ -84,3 +84,13 @@ export class TemplateError extends SandboxError {this.name = 'TemplateError'}}++/**+ * Thrown when the rate limit either for API or sandbox is exceeded.+ */+export class RateLimitError extends SandboxError {+ constructor(message: any) {+ super(message)+ this.name = 'RateLimitError'+ }+}diff --git a/packages/python-sdk/e2b/api/__init__.py b/packages/python-sdk/e2b/api/__init__.pyindex 566d15c8a..fea67173b 100644--- a/packages/python-sdk/e2b/api/__init__.py+++ b/packages/python-sdk/e2b/api/__init__.py@@ -7,7 +7,11 @@ from httpx import HTTPTransport, AsyncHTTPTransportfrom e2b.api.client.client import AuthenticatedClientfrom e2b.connection_config import ConnectionConfigfrom e2b.api.metadata import default_headers-from e2b.exceptions import AuthenticationException, SandboxException+from e2b.exceptions import (+ AuthenticationException,+ SandboxException,+ RateLimitSandboxException,+)from e2b.api.client.types import Responselogger = logging.getLogger(__name__)@@ -19,6 +23,11 @@ def handle_api_exception(e: Response):except json.JSONDecodeError:body = {}+ if e.status_code == 429:+ return RateLimitSandboxException(+ f"{e.status_code}: Rate limit exceeded, please try again later."+ )+if "message" in body:return SandboxException(f"{e.status_code}: {body['message']}")return SandboxException(f"{e.status_code}: {e.content}")diff --git a/packages/python-sdk/e2b/exceptions.py b/packages/python-sdk/e2b/exceptions.pyindex ad1484ff1..3223572ef 100644--- a/packages/python-sdk/e2b/exceptions.py+++ b/packages/python-sdk/e2b/exceptions.py@@ -75,3 +75,9 @@ class TemplateException(SandboxException):"""Exception raised when the template uses old envd version. It isn't compatible with the new SDK."""+++class RateLimitSandboxException(SandboxException):+ """+ Raised when the rate limit either for API or sandbox is exceeded.+ """