11import { Sandbox as BaseSandbox , InvalidArgumentError } from 'e2b'
22
3- import { Result , Execution , OutputMessage , parseOutput , extractError } from './messaging'
3+ import { Result , Execution , OutputMessage , parseOutput , extractError , ExecutionError } from './messaging'
44import { formatExecutionTimeoutError , formatRequestTimeoutError , readLines } from "./utils" ;
55import { JUPYTER_PORT , DEFAULT_TIMEOUT_MS } from './consts'
66export type Context = {
@@ -25,6 +25,7 @@ export class Sandbox extends BaseSandbox {
2525 * @param opts.onStdout Callback for handling stdout messages
2626 * @param opts.onStderr Callback for handling stderr messages
2727 * @param opts.onResult Callback for handling the final result
28+ * @param opts.onError Callback for handling the `ExecutionError` object
2829 * @param opts.envs Environment variables to set for the execution
2930 * @param opts.timeoutMs Max time to wait for the execution to finish
3031 * @param opts.requestTimeoutMs Max time to wait for the request to finish
@@ -37,6 +38,7 @@ async runCode(
3738 onStdout ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
3839 onStderr ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
3940 onResult ?: ( data : Result ) => ( Promise < any > | any ) ,
41+ onError ?: ( error : ExecutionError ) => ( Promise < any > | any ) ,
4042 envs ?: Record < string , string > ,
4143 timeoutMs ?: number ,
4244 requestTimeoutMs ?: number ,
@@ -52,6 +54,7 @@ async runCode(
5254 * @param opts.onStdout Callback for handling stdout messages
5355 * @param opts.onStderr Callback for handling stderr messages
5456 * @param opts.onResult Callback for handling the final result
57+ * @param opts.onError Callback for handling the `ExecutionError` object
5558 * @param opts.envs Environment variables to set for the execution
5659 * @param opts.timeoutMs Max time to wait for the execution to finish
5760 * @param opts.requestTimeoutMs Max time to wait for the request to finish
@@ -64,6 +67,7 @@ async runCode(
6467 onStdout ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
6568 onStderr ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
6669 onResult ?: ( data : Result ) => ( Promise < any > | any ) ,
70+ onError ?: ( error : ExecutionError ) => ( Promise < any > | any ) ,
6771 envs ?: Record < string , string > ,
6872 timeoutMs ?: number ,
6973 requestTimeoutMs ?: number ,
@@ -77,6 +81,7 @@ async runCode(
7781 onStdout ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
7882 onStderr ?: ( output : OutputMessage ) => ( Promise < any > | any ) ,
7983 onResult ?: ( data : Result ) => ( Promise < any > | any ) ,
84+ onError ?: ( error : ExecutionError ) => ( Promise < any > | any ) ,
8085 envs ?: Record < string , string > ,
8186 timeoutMs ?: number ,
8287 requestTimeoutMs ?: number ,
@@ -135,7 +140,7 @@ async runCode(
135140
136141 try {
137142 for await ( const chunk of readLines ( res . body ) ) {
138- await parseOutput ( execution , chunk , opts ?. onStdout , opts ?. onStderr , opts ?. onResult )
143+ await parseOutput ( execution , chunk , opts ?. onStdout , opts ?. onStderr , opts ?. onResult , opts ?. onError )
139144 }
140145 } catch ( error ) {
141146 throw formatExecutionTimeoutError ( error )
0 commit comments