Skip to content

Commit

Permalink
Add rate limit error (#461)
Browse files Browse the repository at this point in the history
# Description

There's only general exception raised right now. I've added specific
exception when the request is denied because of throttling
  • Loading branch information
jakubno authored Oct 26, 2024
2 parents c7c225c + 9fa4eba commit a739ac2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/itchy-clouds-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@e2b/python-sdk': patch
'e2b': patch
---

Add rate limit exception
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.
"""

0 comments on commit a739ac2

Please sign in to comment.