SandboxJS icon indicating copy to clipboard operation
SandboxJS copied to clipboard

How to get the line number of an error when executing code in the sandbox?

Open IamSanjid opened this issue 2 years ago • 2 comments

const sandbox = new Sandbox();

const code = `function onStart() {
    console.log(some_undefined_var);
}
return { onStart: onStart };
`;

const exec = sandbox.compile(code);
const script_context = exec({}).run();

// here an error should occur...
script_context['onStart']();

So I would like to know at which line(maybe?) the error occurred while executing the code in the provided coding string, I think I can get the function name in a way but need some examples of how to approach this.

IamSanjid avatar Mar 21 '23 10:03 IamSanjid

The sandbox currently does not keep track of stack trace, so it provides a code snippet where the issue is.

nyariv avatar Mar 27 '23 09:03 nyariv

The sandbox currently does not keep track of stack trace, so it provides a code snippet where the issue is.

How can I retrieve that code snippet? Normal try-catch doesn't seem to be helping any example would be helpful.

IamSanjid avatar Mar 28 '23 12:03 IamSanjid