Skip to content
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

Handle error in async callback #53

Closed
vshymanskyy opened this issue Feb 3, 2017 · 9 comments
Closed

Handle error in async callback #53

vshymanskyy opened this issue Feb 3, 2017 · 9 comments

Comments

@vshymanskyy
Copy link

Is there any way to handle error in async callback?

const {NodeVM} = require('vm2');

const vm = new NodeVM({
    timeout: 10000,
    sandbox: {}
});

try {
  vm.run(`
    setInterval(() => {
      console.log("haha")
      process.exit()              // <--- can't find a way to intercept this!
    }, 1000);

    process.exit()
  `, 'code.js');
} catch(e) {
  console.log(e.message);
}


setInterval(() => {
  console.log("haha 2")
},1000);

I'm seeking for a way to prevent whole script crashing.

@patriksimek
Copy link
Owner

You can handle asynchronous errors like this:

process.on('uncaughtException', (err) => {
	console.log(err);
})

Keep in mind that this catches all exceptions from both inside and outside of the VM.

@vshymanskyy
Copy link
Author

Thx. Yes, it partially helped me. BTW, is there any way to distinguish if error in 'uncaughtException' is related to a VM (and possibly, which of them) or not?

@patriksimek
Copy link
Owner

Unfortunately not at the moment. I have an idea how to implement this but it will take some time.

@alsterg
Copy link

alsterg commented May 11, 2017

I'm wrapping vm.run() with domain.run() and it seems to work. I don't know if it has any side effects though..

let domain = Domain.create();
domain.on('error', (err) => {
    console.error('Asynchronous error while executing script.', err.stack);
});
domain.run(() => {
    try {
        vm.run(script, filename);
    } catch (err) {
        console.error('Synchronous error while executing script.', err.stack);
    }
});

@stale
Copy link

stale bot commented Jan 27, 2019

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.

@stale stale bot added the stale label Jan 27, 2019
@stale stale bot closed this as completed Feb 3, 2019
@ImSingee
Copy link

ImSingee commented Feb 1, 2020

@alsterg The method seems to lose effectiveness..

@patriksimek Could you please tell me if you forgot about this?

@janhaa
Copy link

janhaa commented Sep 9, 2020

Confirming @ImSingee, method unfortunately does not work anymore.
Is there any update on how to handle async errors?

@AshuInsideOut
Copy link

Any updates on this???

@rob-gordon
Copy link

This is pretty messy so I'm not recommending anyone do this, but I'm posting the way I got around this here. I wanted my scripts to be able to contain await so I was already wrapping the scripts in (async () => { ... })()

To catch errors inside the async IIFE, I used another IIFE inside a try/catch to store the result, then returned a status key to tell me if there was an error or not.

(async () => {
  try {
    let result = (() => {
      // script here
    })();
    return { status: "success", result: result };
  } catch (e) {
    return { status: "error", error: e.message };
  }
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants