Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Handle rate limit errors in JS SDK
Jakub Novak6bd8212f4285
diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.tsindex 6747bca84..ad49d2b90 100644--- a/packages/js-sdk/src/api/index.ts+++ b/packages/js-sdk/src/api/index.ts@@ -1,4 +1,4 @@-import createClient from 'openapi-fetch'+import createClient, { FetchResponse } from 'openapi-fetch'import type { components, paths } from './schema.gen'import { defaultHeaders } from './metadata'@@ -6,12 +6,15 @@ import { ConnectionConfig } from '../connectionConfig'import { AuthenticationError, SandboxError } from '../errors'import { createApiLogger } from '../logs'-export function handleApiError(err?: { code: number; message: string }) {- if (!err) {+export function handleApiError(+ response: FetchResponse<any, any, any>+): Error | undefined {+ if (!response.error) {return}- return new SandboxError(`${err.code}: ${err.message}`)+ const message = response.error?.message ?? response.error+ return new SandboxError(`${response.response.status}: ${message}`)}/**@@ -30,15 +33,15 @@ class ApiClient {if (!opts?.requireApiKey && !config.apiKey) {throw new AuthenticationError('API key is required, please visit https://e2b.dev/docs to get your API key. ' +- 'You can either set the environment variable `E2B_API_KEY` ' +- "or you can pass it directly to the sandbox like Sandbox.create({ apiKey: 'e2b_...' })"+ 'You can either set the environment variable `E2B_API_KEY` ' ++ "or you can pass it directly to the sandbox like Sandbox.create({ apiKey: 'e2b_...' })")}if (opts?.requireAccessToken && !config.accessToken) {throw new AuthenticationError('Access token is required, please visit https://e2b.dev/docs to get your access token. ' +- 'You can set the environment variable `E2B_ACCESS_TOKEN` or pass the `accessToken` in options.'+ 'You can set the environment variable `E2B_ACCESS_TOKEN` or pass the `accessToken` in options.')}diff --git a/packages/js-sdk/src/sandbox/sandboxApi.ts b/packages/js-sdk/src/sandbox/sandboxApi.tsindex 9d4cd1fcd..1cf997f4d 100644--- a/packages/js-sdk/src/sandbox/sandboxApi.ts+++ b/packages/js-sdk/src/sandbox/sandboxApi.ts@@ -9,7 +9,7 @@ import { TemplateError } from '../errors'export interface SandboxApiOptsextends Partial<Pick<ConnectionOpts, 'apiKey' | 'debug' | 'domain' | 'requestTimeoutMs'>- > { }+ > {}/*** Information about a sandbox.@@ -42,14 +42,14 @@ export interface SandboxInfo {}export class SandboxApi {- protected constructor() { }+ protected constructor() {}/*** Kill the sandbox specified by sandbox ID.** @param sandboxId sandbox ID.* @param opts connection options.- *+ ** @returns `true` if the sandbox was found and killed, `false` otherwise.*/static async kill(@@ -72,7 +72,7 @@ export class SandboxApi {return false}- const err = handleApiError(res.error)+ const err = handleApiError(res)if (err) {throw err}@@ -84,7 +84,7 @@ export class SandboxApi {* List all running sandboxes.** @param opts connection options.- *+ ** @returns list of running sandboxes.*/static async list(opts?: SandboxApiOpts): Promise<SandboxInfo[]> {@@ -95,7 +95,7 @@ export class SandboxApi {signal: config.getSignal(opts?.requestTimeoutMs),})- const err = handleApiError(res.error)+ const err = handleApiError(res)if (err) {throw err}@@ -117,11 +117,11 @@ export class SandboxApi {/*** Set the timeout of the specified sandbox.* After the timeout expires the sandbox will be automatically killed.- *+ ** This method can extend or reduce the sandbox timeout set when creating the sandbox or from the last call to {@link Sandbox.setTimeout}.- *+ ** Maximum time a sandbox can be kept alive is 24 hours (86_400_000 milliseconds) for Pro users and 1 hour (3_600_000 milliseconds) for Hobby users.- *+ ** @param sandboxId sandbox ID.* @param timeoutMs timeout in **milliseconds**.* @param opts connection options.@@ -146,7 +146,7 @@ export class SandboxApi {signal: config.getSignal(opts?.requestTimeoutMs),})- const err = handleApiError(res.error)+ const err = handleApiError(res)if (err) {throw err}@@ -173,7 +173,7 @@ export class SandboxApi {signal: config.getSignal(opts?.requestTimeoutMs),})- const err = handleApiError(res.error)+ const err = handleApiError(res)if (err) {throw err}@@ -188,7 +188,7 @@ export class SandboxApi {)throw new TemplateError('You need to update the template to use the new SDK. ' +- 'You can do this by running `e2b template build` in the directory with the template.'+ 'You can do this by running `e2b template build` in the directory with the template.')}return this.getSandboxId({diff --git a/packages/js-sdk/tests/setup.ts b/packages/js-sdk/tests/setup.tsindex 683a6ecea..7cdbd5e0c 100644--- a/packages/js-sdk/tests/setup.ts+++ b/packages/js-sdk/tests/setup.ts@@ -9,17 +9,18 @@ interface SandboxFixture {export const sandboxTest = base.extend<SandboxFixture>({sandbox: [- async ({ }, use) => {+ async ({}, use) => {const sandbox = await Sandbox.create(template)try {await use(sandbox)-} finally {try {await sandbox.kill()} catch (err) {if (!isDebug) {- console.warn('Failed to kill sandbox — this is expected if the test runs with local envd.')+ console.warn(+ 'Failed to kill sandbox — this is expected if the test runs with local envd.'+ )}}}