-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement custom error handling #230
Conversation
Example code: const { VM } = require('vm2');
const sourceCode = `function f() {
null.f();
}
f();`;
const vm = new VM({
prettyErrors: true,
});
vm.run(sourceCode); |
Just an idea... what if we implement stack prettifying on the proxy level? All errors that come from the sandbox are decontextified (via Proxy). That way we don't have to use uncaughtException handler and it also can fix stack traces for caught exceptions. As I said, it's an idea... not sure if that is possible, but I think it is worth investigating. |
Also, using a Domain may be a better way to catch uncaught exceptions from the VM. The API is deprecated, but the replacement API was not introduced yet so it should be fine to use it. |
I'm not sure about moving the code to the decontextifier, it would be a bit messy to pass the settings and script name without significant gain. It would be nice not to register a custom uncaughtException handler, though: how do you feel about moving this code to the catch block? Something like: } catch (dirtyEx) {
const e = this._internal.Decontextify.value(dirtyEx);
if (!this.options.prettyErrors)
throw e;
let rewrittenEx = new Error();
a.stack = ...;
throw rewrittenEx;
} |
ping |
Sorry for the delay. The issue with your proposal is you'll lose all properties available on the original error object. The goal is to only rewrite the stack property. What about using a Proxy with a get handler just for the |
I'm trying to do it in Decontextify, but I can't seem to modify the returned stack. Do you have any pointers in that regard? I'm clueless. I removed my code from the catch block and added the following in Decontextify.instance.get: if (key === 'stack' && target instanceof Error) {
// throw 1;
a = new Error();
return a.stack; If you uncomment the |
I was able to trigger the if (instance instanceof Error && key === 'stack') {
return prettify(instance.stack);
} Don't forget to put that in the try-catch block. One more thing - you don't have to rewrite the file name, you can define the name of the script with this code: console.log(vm.run(new VMScript(`new Error('asdf')`, 'myscript.js')).stack); The stack trace is then generated with the correct file name. |
@CapacitorSet thanks for the work on this. Just got bit by a hard-to-debug error due to no stacktrace, and I'd love to see this feature added! Wanted to gently follow-up to see if anything is blocking this (other than lack of time)? |
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. |
Commenting to keep open |
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. |
@CapacitorSet @patriksimek let me know if I can help? |
For some reason I couldn't apply the fix suggested by Patrik, and eventually abandoned the issue. @tbenst could you look into that? |
Commenting here. I think it would be great if this could feature could be implemented. |
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. |
As discussed in #87. Produces stack traces that look the same as "native ones", with vm2 frames stripped. Controlled by the
prettyErrors
option for VM, true by default.With no pretty errors
With pretty errors