简体   繁体   中英

Throwing a Meteor.Error from a server method causes script to exit

I am throwing a Meteor.Error exception from a server-side method.

throw new Meteor.Error( 500, 'There was an error processing your request' );

My goal is get the client side Meteor.call to receive this error, which it does, but throwing also causes the node process to exit.

error: Forever detected script exited with code: 8

What is the correct way to signal errors from a Meteor.methods() to a Meteor.call without killing the script?

This can happen if you throw your method from outside the fiber of your method somehow. For example

Meteor.methods({
    test: function() {
        setTimeout(function() {
            throw new Meteor.Error( 500, 'There was an error processing your request' );
        }, 0);
    }
});

If you are using something that can escape the fiber the method is running in it can cause Meteor to exit.

You just need to make sure where you throw the error it is inside the fiber. (eg in the above example you can use Meteor.setTimeout instead of setTimeout .

If you are using an npm module you should use Meteor.bindEnvironment for the callbacks. or Meteor.wrapAsync to ensure that the callbacks run in the same fiber.

Once you do this your app should not crash and won't cause forevever to restart it.

The first argument should be a string not an integer in meteor 1.2.1

http://docs.meteor.com/#/full/meteor_error

Try this:

Meteor.methods({
  "foo":function(){
    try{
      var id = Clients.insert(client);
      if(id){
        return id;
      }
    }catch(e){
      throw new Meteor.Error(400,e.message);
    } 
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM