Skip to main content
Version: 0.1.22
Last Verified: 2025-01-27
Package: @hopx-ai/sdk on npm

Overview

The JavaScript SDK provides a comprehensive error hierarchy for handling different types of errors. All errors inherit from HopxError.

Import

import {
    HopxError,
    APIError,
    AuthenticationError,
    NotFoundError,
    ValidationError,
    RateLimitError,
    ResourceLimitError,
    ServerError,
    NetworkError,
    TimeoutError
} from '@hopx-ai/sdk';

Base Error

HopxError

Base exception for all HopX SDK errors. Properties:
  • message (string): Error message
  • code (string, optional): Error code
  • requestId (string, optional): Request ID for debugging
  • statusCode (number, optional): HTTP status code
  • details (Record<string, any>, optional): Additional error details

API Errors

APIError

Base class for API request failures. Inherits from: HopxError

AuthenticationError

Authentication failed (401). Inherits from: APIError When thrown:
  • Invalid or missing API key
  • Expired authentication token

NotFoundError

Resource not found (404). Inherits from: APIError When thrown:
  • Sandbox not found
  • Template not found
  • File not found

ValidationError

Request validation failed (400). Inherits from: APIError When thrown:
  • Invalid parameters
  • Missing required fields

RateLimitError

Rate limit exceeded (429). Inherits from: APIError Properties:
  • retryAfter (number, optional): Seconds to wait before retrying

ResourceLimitError

Resource limit exceeded. Inherits from: APIError Properties:
  • limit (number): Resource limit
  • current (number): Current usage
  • available (number): Available resources

ServerError

Server error (5xx). Inherits from: APIError

Network Errors

NetworkError

Network communication failed. Inherits from: HopxError

TimeoutError

Request timed out. Inherits from: NetworkError

Error Handling Examples

Example 1: Basic Error Handling

import { Sandbox, HopxError } from '@hopx-ai/sdk';

try {
    const sandbox = await Sandbox.create({ template: 'code-interpreter' });
    const result = await sandbox.runCode("print('Hello')");
} catch (error) {
    if (error instanceof HopxError) {
        console.error(`Error: ${error.message}`);
        if (error.statusCode) {
            console.error(`Status: ${error.statusCode}`);
        }
    }
}

Example 2: Specific Error Handling

import { Sandbox, AuthenticationError, NotFoundError } from '@hopx-ai/sdk';

try {
    const sandbox = await Sandbox.create({ template: 'code-interpreter' });
} catch (error) {
    if (error instanceof AuthenticationError) {
        console.error('Authentication failed. Check your API key.');
    } else if (error instanceof NotFoundError) {
        console.error('Template not found.');
    }
}

  • Types - TypeScript type definitions
  • Sandbox - Sandbox operations that may throw errors

See Also

Next Steps

Now that you understand error handling, explore: