Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate limit error #461

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/js-sdk/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -13,6 +13,10 @@ export function handleApiError(
return
}

if (response.response.status === 429) {
return new RateLimitError('Rate limit exceeded, please try again later.')
}

const message = response.error?.message ?? response.error
return new SandboxError(`${response.response.status}: ${message}`)
}
Expand Down
20 changes: 15 additions & 5 deletions packages/js-sdk/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -84,3 +84,13 @@ export class TemplateError extends SandboxError {
this.name = 'TemplateError'
}
}

/**
* Thrown when the API rate limit is exceeded.
*/
export class RateLimitError extends SandboxError {
constructor(message: any) {
super(message)
this.name = 'RateLimitError'
}
}
11 changes: 10 additions & 1 deletion packages/python-sdk/e2b/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from e2b.api.client.client import AuthenticatedClient
from e2b.connection_config import ConnectionConfig
from e2b.api.metadata import default_headers
from e2b.exceptions import AuthenticationException, SandboxException
from e2b.exceptions import (
AuthenticationException,
SandboxException,
RateLimitException,
)
from e2b.api.client.types import Response

logger = logging.getLogger(__name__)
Expand All @@ -19,6 +23,11 @@ def handle_api_exception(e: Response):
except json.JSONDecodeError:
body = {}

if e.status_code == 429:
return RateLimitException(
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}")
Expand Down
6 changes: 6 additions & 0 deletions packages/python-sdk/e2b/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 RateLimitException(SandboxException):
"""
Raised when the API rate limit is exceeded.
"""