Skip to main content

Error Handling

The SDK raises its own exceptions for authentication, permission, rate-limit and server errors. Catch them to handle failures.

Exception types​

from instavm import (
InstaVM,
InstaVMError,
AuthenticationError,
ForbiddenError,
SessionError,
ExecutionError,
NetworkError,
RateLimitError,
BrowserError,
BrowserSessionError,
BrowserInteractionError,
BrowserTimeoutError,
BrowserNavigationError,
ElementNotFoundError,
)

How HTTP errors map to exceptions​

StatusException
401AuthenticationError
403ForbiddenError
429RateLimitError
500, 502, 503, 504, timeouts, connection errorsNetworkError
Other 4xx (400, 402, 404, 409, 422, ...)requests.exceptions.HTTPError from most methods

execute() wraps every error that is not an InstaVMError in ExecutionError. Browser methods wrap errors in BrowserError subclasses.

ForbiddenError and RateLimitError carry the API's detail as e.args[0]. For some 403 errors that is a dict with a code, such as card_required. See Errors for the codes.

Code execution errors​

try:
vm = InstaVM('your_api_key')
result = vm.execute("print('hello')")

except AuthenticationError:
print("Invalid API key")

except ForbiddenError as e:
print(f"Not allowed: {e.args[0]}")

except RateLimitError as e:
print(f"Rate limited: {e}")

except SessionError as e:
print(f"Session error: {e}")

except ExecutionError as e:
print(f"Code execution failed: {e}")

except NetworkError as e:
print(f"Network issue: {e}")

Management API errors​

VM, snapshot and other management methods raise requests.exceptions.HTTPError for 4xx statuses the SDK does not map. Read the status and body from e.response:

import requests

client = InstaVM('your_api_key')

try:
vm = client.vms.get("vmid-does-not-exist")
except requests.exceptions.HTTPError as e:
print(e.response.status_code) # 404
print(e.response.json()["detail"]) # "VM not found"

Browser errors​

try:
client = InstaVM('your_api_key')
with client.browser.create_session() as session:
session.navigate("https://example.com")
session.click("button.nonexistent")

except ElementNotFoundError as e:
print(f"Element not found: {e}")

except BrowserTimeoutError as e:
print(f"Timed out: {e}")

except BrowserNavigationError as e:
print(f"Navigation failed: {e}")

except BrowserError as e:
print(f"Browser error: {e}")

A 429 while creating a browser session surfaces as BrowserSessionError.

Exception hierarchy​

InstaVMError (base)
├── AuthenticationError
├── ForbiddenError
├── SessionError
├── ExecutionError
├── NetworkError
├── RateLimitError
├── QuotaExceededError
├── UnsupportedOperationError
├── CustomDomainError
└── BrowserError
├── BrowserSessionError
├── BrowserInteractionError
│ └── ElementNotFoundError
├── BrowserTimeoutError
└── BrowserNavigationError

Catching BrowserError catches all browser exceptions. Catching InstaVMError catches every SDK exception, but not requests.exceptions.HTTPError.

Retries​

max_retries defaults to 0. With InstaVM(api_key, max_retries=N), the SDK retries 5xx responses, timeouts and connection errors up to N times, waiting 1, 2, 4, ... seconds. It never retries 401, 403 or 429.

The SDK retries every method, including vms.create() and execute(). A retried create can start a second VM, and a retried execute() can run the command twice. See which requests are safe to repeat.

To retry rate-limited calls yourself:

import random
import time
from instavm import RateLimitError

def with_backoff(call, attempts=5):
for attempt in range(attempts):
try:
return call()
except RateLimitError:
if attempt == attempts - 1:
raise
time.sleep(min(2 ** attempt, 30) + random.random())

vms = with_backoff(client.vms.list)

A 429 for the concurrent VM limit does not clear by waiting. See Rate limits.

Next steps​