Skip to content

armgit / E2B

Public Imported from GitHub
0

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

fix: JS should return an error if body is empty (#1114)

Jakub Dobry1780a0cc6b89
diff --git a/.changeset/fix-empty-error-body-handling.md b/.changeset/fix-empty-error-body-handling.md
new file mode 100644
index 000000000..c57d629a3
--- /dev/null
+++ b/.changeset/fix-empty-error-body-handling.md
@@ -0,0 +1,5 @@
+---
+"e2b": patch
+---
+
+Fix error handling for API responses with empty body.
diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.ts
index faa38cc0d..0e7895df1 100644
--- a/packages/js-sdk/src/api/index.ts
+++ b/packages/js-sdk/src/api/index.ts
@@ -14,7 +14,9 @@ export function handleApiError(
) => Error = SandboxError,
stackTrace?: string
): Error | undefined {
- if (!response.error) {
+ // openapi-fetch returns empty string for error when response body is empty,
+ // so we check !== undefined instead of truthiness
+ if (response.error === undefined) {
return
}
diff --git a/packages/js-sdk/tests/api/handleApiError.test.ts b/packages/js-sdk/tests/api/handleApiError.test.ts
new file mode 100644
index 000000000..1dc1e855e
--- /dev/null
+++ b/packages/js-sdk/tests/api/handleApiError.test.ts
@@ -0,0 +1,108 @@
+import { assert, test, describe } from 'vitest'
+import { handleApiError } from '../../src/api'
+import {
+ AuthenticationError,
+ RateLimitError,
+ SandboxError,
+} from '../../src/errors'
+
+function createMockResponse(
+ status: number,
+ error: unknown,
+ data?: unknown
+): {
+ response: { status: number; ok: boolean }
+ error: unknown
+ data: unknown
+} {
+ return {
+ response: { status, ok: status >= 200 && status < 300 },
+ error,
+ data,
+ }
+}
+
+describe('handleApiError', () => {
+ describe('with empty error body', () => {
+ test('catches 404 with empty string error', () => {
+ const res = createMockResponse(404, '')
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, SandboxError)
+ assert.include(err?.message, '404')
+ })
+
+ test('catches 400 with empty string error', () => {
+ const res = createMockResponse(400, '')
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, SandboxError)
+ assert.include(err?.message, '400')
+ })
+
+ test('catches 500 with empty string error', () => {
+ const res = createMockResponse(500, '')
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, SandboxError)
+ assert.include(err?.message, '500')
+ })
+ })
+
+ describe('with JSON error body', () => {
+ test('catches 404 with message', () => {
+ const res = createMockResponse(404, { code: 404, message: 'Not found' })
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, SandboxError)
+ assert.include(err?.message, 'Not found')
+ })
+
+ test('catches 400 with message', () => {
+ const res = createMockResponse(400, { code: 400, message: 'Bad request' })
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, SandboxError)
+ assert.include(err?.message, 'Bad request')
+ })
+ })
+
+ describe('special status codes', () => {
+ test('returns AuthenticationError for 401', () => {
+ const res = createMockResponse(401, { message: 'Invalid token' })
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, AuthenticationError)
+ assert.include(err?.message, 'Unauthorized')
+ })
+
+ test('returns AuthenticationError for 401 with empty body', () => {
+ const res = createMockResponse(401, '')
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, AuthenticationError)
+ assert.include(err?.message, 'Unauthorized')
+ })
+
+ test('returns RateLimitError for 429', () => {
+ const res = createMockResponse(429, { message: 'Too many requests' })
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, RateLimitError)
+ assert.include(err?.message, 'Rate limit')
+ })
+
+ test('returns RateLimitError for 429 with empty body', () => {
+ const res = createMockResponse(429, '')
+ const err = handleApiError(res as any)
+ assert.instanceOf(err, RateLimitError)
+ assert.include(err?.message, 'Rate limit')
+ })
+ })
+
+ describe('success responses', () => {
+ test('returns undefined for 200 success', () => {
+ const res = createMockResponse(200, undefined, { id: '123' })
+ const err = handleApiError(res as any)
+ assert.isUndefined(err)
+ })
+
+ test('returns undefined for 201 success', () => {
+ const res = createMockResponse(201, undefined, { id: '123' })
+ const err = handleApiError(res as any)
+ assert.isUndefined(err)
+ })
+ })
+})