Skip to content

Commit d40a189

Browse files
mishushakovclaude
andcommitted
Merge sandbox-killed check and request timeout formatting
Consolidate the two-line catch handler into a single formatRequestError call that returns the error to throw, matching the main SDK pattern (e2b-dev/E2B#1419). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 17cce6a commit d40a189

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

js/src/sandbox.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ export class Sandbox extends BaseSandbox {
279279

280280
return execution
281281
} catch (error) {
282-
await this.throwIfSandboxKilled(error)
283-
throw formatRequestTimeoutError(error)
282+
throw await this.formatRequestError(error)
284283
}
285284
}
286285

@@ -319,8 +318,7 @@ export class Sandbox extends BaseSandbox {
319318

320319
return await res.json()
321320
} catch (error) {
322-
await this.throwIfSandboxKilled(error)
323-
throw formatRequestTimeoutError(error)
321+
throw await this.formatRequestError(error)
324322
}
325323
}
326324

@@ -356,8 +354,7 @@ export class Sandbox extends BaseSandbox {
356354
throw error
357355
}
358356
} catch (error) {
359-
await this.throwIfSandboxKilled(error)
360-
throw formatRequestTimeoutError(error)
357+
throw await this.formatRequestError(error)
361358
}
362359
}
363360

@@ -392,8 +389,7 @@ export class Sandbox extends BaseSandbox {
392389

393390
return await res.json()
394391
} catch (error) {
395-
await this.throwIfSandboxKilled(error)
396-
throw formatRequestTimeoutError(error)
392+
throw await this.formatRequestError(error)
397393
}
398394
}
399395

@@ -429,29 +425,30 @@ export class Sandbox extends BaseSandbox {
429425
throw error
430426
}
431427
} catch (error) {
432-
await this.throwIfSandboxKilled(error)
433-
throw formatRequestTimeoutError(error)
428+
throw await this.formatRequestError(error)
434429
}
435430
}
436431

437432
/**
438-
* Throws a descriptive `TimeoutError` if the connection error was caused
439-
* by the sandbox being killed mid-request. If the sandbox is still running
440-
* (or its state can't be determined), returns so the caller can re-throw
441-
* the original error.
433+
* Returns the error to throw for a failed request. If the connection was
434+
* closed because the sandbox was killed mid-request, returns a descriptive
435+
* `TimeoutError`. Otherwise falls back to formatting request timeouts and
436+
* re-throwing the original error.
442437
*/
443-
private async throwIfSandboxKilled(error: unknown): Promise<void> {
438+
private async formatRequestError(error: unknown): Promise<unknown> {
444439
if (
445440
isConnectionClosedError(error) &&
446441
// If the state check itself fails we can't tell whether the sandbox
447-
// was killed — assume it's running so the caller re-throws the
448-
// original error instead of wrongly claiming the sandbox is gone.
442+
// was killed — assume it's running so we re-throw the original error
443+
// instead of wrongly claiming the sandbox is gone.
449444
(await this.isRunning().catch(() => true)) === false
450445
) {
451-
throw new TimeoutError(
446+
return new TimeoutError(
452447
'The sandbox was killed while the request was in progress. This can happen when the sandbox times out or is killed manually. ' +
453448
"You can modify the sandbox timeout by passing 'timeoutMs' when starting the sandbox or calling '.setTimeout' on the sandbox with the desired timeout."
454449
)
455450
}
451+
452+
return formatRequestTimeoutError(error)
456453
}
457454
}

0 commit comments

Comments
 (0)