-
Notifications
You must be signed in to change notification settings - Fork 297
Add support for "correct" stack traces for errors in the sandbox #87
Comments
This would be awesome. I'm trying to track down a coding mistake right now and having a fiendishly hard time without the line number |
If you're running test cases that you know to be safe, you can temporarily replace vm2 with the native vm to track down the error. |
I'd like this feature too. Happy to pitch in to write it if someone points me in the right direction. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Turns out it’s not trivial to replace vm2 with Native vm as there are api incompatibilities, hope this can stay open! |
@patriksimek, I devised a simple implementation based on an exception parser/rewriter. I think it would be worth it to integrate it in vm2 as an optional error handler. const StackTracey = require ('stacktracey');
const { VM } = require('vm2');
const sourceCode = `function f() {
throw Exception('e');
}
f();`;
const scriptName = "hello_world.js";
process.on("uncaughtException", e => {
if (!e.stack.includes("/node_modules/vm2/")) {
// This is not a vm2 error, so print it normally
console.log(e);
return;
}
const oldStack = new StackTracey(e);
const newStack = [];
for (const line of oldStack) {
// Discard internal code
if (line.file.includes("/cjs"))
continue;
if (line.thirdParty && line.file.includes("/node_modules/vm2/"))
continue;
if (line.callee == "Script.runInContext")
continue;
// Replace the default filename with the user-provided one
if (line.fileName == "vm.js")
line.fileRelative = line.fileShort = line.fileName = scriptName;
newStack.push(line);
}
console.log("[vm2] A clean stack trace follows:");
console.log(new StackTracey(newStack).pretty);
});
const vm = new VM();
vm.run(sourceCode, scriptName); The above code would print:
|
I haven't had a chance to test this library but I recently used the vm module and accomplished accurate stack trace numbers by setting the appropriate line offset when creating a new vm.Script. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Figured I’d comment as this issue + suggestions are still relevant for me! |
Very relevant to me too. @patriksimek can @CapacitorSet's implementation or something along those lines be integrated in the project? |
Patrik, does the implementation sketch look okay overall? If so I can make a PR and we can work from there. |
@CapacitorSet Yes, I'll be happy to review a PR. Some notes:
|
Just leaving a comment to notify people who are subscribed to this issue -- I opened a PR, so you can track the progress there. |
It would be nice if vm2 could display "correct" stack traces when an error is thrown from the sandbox.
For instance, this is what is currently displayed:
Is there a way to figure out which line (in the sandboxed code) was responsible for calling the function that threw the error?
The text was updated successfully, but these errors were encountered: