Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Handle error for starting process in folder that doesn't exist
Jakub Novak1a2502a76f5d
diff --git a/.changeset/six-camels-think.md b/.changeset/six-camels-think.mdnew file mode 100644index 000000000..95fab74d6--- /dev/null+++ b/.changeset/six-camels-think.md@@ -0,0 +1,6 @@+---+"@e2b/python-sdk": patch+"@e2b/sdk": patch+---++Handle starting the process in nonexistent folder with better description and dedicated errordiff --git a/packages/js-sdk/src/error.ts b/packages/js-sdk/src/error.tsindex b91f1fb02..c3b575f27 100644--- a/packages/js-sdk/src/error.ts+++ b/packages/js-sdk/src/error.ts@@ -11,3 +11,10 @@ export class AuthenticationError extends Error {this.name = 'AuthenticationError'}}++export class CurrentWorkingDirectoryDoesntExistError extends Error {+ constructor(message: any) {+ super(message)+ this.name = 'CurrentWorkingDirectoryDoesntExistError'+ }+}\ No newline at end of filediff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.tsindex 894822321..f7acdbf7b 100644--- a/packages/js-sdk/src/sandbox/index.ts+++ b/packages/js-sdk/src/sandbox/index.ts@@ -10,6 +10,7 @@ import { Process, ProcessManager, ProcessMessage, ProcessOpts, ProcessOutput, primport { CallOpts, SandboxConnection, SandboxConnectionOpts } from './sandboxConnection'import { Terminal, TerminalManager, TerminalOpts, TerminalOutput, terminalService } from './terminal'import { resolvePath } from '../utils/filesystem'+import { CurrentWorkingDirectoryDoesntExistError } from '../error'export type DownloadFileFormat =| 'base64'@@ -387,6 +388,11 @@ export class Sandbox extends SandboxConnection {} catch (err) {triggerExit()await unsubscribing+ if (/error starting process '\w+': fork\/exec \/bin\/bash: no such file or directory/.test((err as Error)?.message)) {+ throw new CurrentWorkingDirectoryDoesntExistError(+ `Failed to start the process. You are trying set 'cwd' to a directory that does not exist.\n${(err as Error)?.message}`+ )+ }throw err}diff --git a/packages/js-sdk/test/cwd.test.mjs b/packages/js-sdk/test/cwd.test.mjsindex 2167a2552..758481a05 100644--- a/packages/js-sdk/test/cwd.test.mjs+++ b/packages/js-sdk/test/cwd.test.mjs@@ -2,6 +2,7 @@ import { Sandbox } from '../src'import { expect, test, vi } from 'vitest'import { id } from './setup.mjs'+import { CurrentWorkingDirectoryDoesntExistError } from '../src/error'test('custom cwd', async () => {const sandbox = await Sandbox.create({@@ -101,3 +102,9 @@ test('test_warnings', async () => {expect(console.warn).toHaveBeenCalledTimes(3)await sandbox.close()})++test('test_non_existing_cwd', async () => {+ const sandbox = await Sandbox.create({ id })+ await expect(sandbox.process.start({ cmd: 'pwd', cwd: '/this/does/not/exist' })).rejects.toThrow(CurrentWorkingDirectoryDoesntExistError)+ await sandbox.close()+})diff --git a/packages/js-sdk/testground/basic.cjs b/packages/js-sdk/testground/basic.cjsindex 3858d74fd..7ba7915a8 100644--- a/packages/js-sdk/testground/basic.cjs+++ b/packages/js-sdk/testground/basic.cjs@@ -2,7 +2,7 @@ const e2b = require('../dist')async function main() {const sandbox = await e2b.Sandbox.create({- id: 'Nodejs',+ id: 'base',apiKey: process.env.E2B_API_KEY})diff --git a/packages/python-sdk/e2b/__init__.py b/packages/python-sdk/e2b/__init__.pyindex e98a69bb5..dd4537171 100644--- a/packages/python-sdk/e2b/__init__.py+++ b/packages/python-sdk/e2b/__init__.py@@ -22,6 +22,7 @@ from .sandbox import (SandboxException,TerminalException,ProcessException,+ CurrentWorkingDirectoryDoesntExistException,FilesystemException,RpcException,ProcessMessage,diff --git a/packages/python-sdk/e2b/sandbox/__init__.py b/packages/python-sdk/e2b/sandbox/__init__.pyindex d523d64bc..36125702a 100644--- a/packages/python-sdk/e2b/sandbox/__init__.py+++ b/packages/python-sdk/e2b/sandbox/__init__.py@@ -1,6 +1,7 @@from .code_snippet import OpenPortfrom .env_vars import EnvVarsfrom .exception import (+ CurrentWorkingDirectoryDoesntExistException,FilesystemException,ProcessException,RpcException,diff --git a/packages/python-sdk/e2b/sandbox/exception.py b/packages/python-sdk/e2b/sandbox/exception.pyindex c9230e1e0..39dd04cf2 100644--- a/packages/python-sdk/e2b/sandbox/exception.py+++ b/packages/python-sdk/e2b/sandbox/exception.py@@ -34,6 +34,10 @@ class ProcessException(SandboxException):pass+class CurrentWorkingDirectoryDoesntExistException(ProcessException):+ pass++class TerminalException(SandboxException):passdiff --git a/packages/python-sdk/e2b/sandbox/process.py b/packages/python-sdk/e2b/sandbox/process.pyindex 0491408d2..486023729 100644--- a/packages/python-sdk/e2b/sandbox/process.py+++ b/packages/python-sdk/e2b/sandbox/process.py@@ -1,4 +1,5 @@import logging+import refrom concurrent.futures import ThreadPoolExecutorfrom typing import (Any,@@ -13,7 +14,12 @@ from pydantic import BaseModelfrom e2b.constants import TIMEOUTfrom e2b.sandbox.env_vars import EnvVars-from e2b.sandbox.exception import MultipleExceptions, ProcessException, RpcException+from e2b.sandbox.exception import (+ MultipleExceptions,+ ProcessException,+ RpcException,+ CurrentWorkingDirectoryDoesntExistException,+)from e2b.sandbox.out import OutStderrResponse, OutStdoutResponsefrom e2b.sandbox.sandbox_connection import SandboxConnectionfrom e2b.utils.future import DeferredFuture@@ -366,6 +372,13 @@ class ProcessManager:)except RpcException as e:trigger_exit()+ if re.match(+ r"error starting process '\w+': fork/exec /bin/bash: no such file or directory",+ e.message,+ ):+ raise CurrentWorkingDirectoryDoesntExistException(+ "Failed to start the process. You are trying set `cwd` to a directory that does not exist."+ ) from eraise ProcessException(e.message) from eexcept TimeoutError as e:logger.error(f"Timeout error during starting the process: {cmd}")diff --git a/packages/python-sdk/tests/test_cwd.py b/packages/python-sdk/tests/test_cwd.pyindex 949297f4e..6d4445bb7 100644--- a/packages/python-sdk/tests/test_cwd.py+++ b/packages/python-sdk/tests/test_cwd.py@@ -1,5 +1,5 @@import pytest-from e2b import Sandbox+from e2b import CurrentWorkingDirectoryDoesntExistException, Sandboxdef test_process_cwd():@@ -82,3 +82,12 @@ def test_warnings():sandbox.filesystem.write("~/hello.txt", "Hello Vasek!")sandbox.close()+++def test_doesnt_exists():+ sandbox = Sandbox()++ with pytest.raises(CurrentWorkingDirectoryDoesntExistException):+ sandbox.process.start("ls", cwd="/this/doesnt/exist")++ sandbox.close()