10

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?

0x6A75616E
  • 4,696
  • 2
  • 33
  • 57

3 Answers3

14

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. (e.g 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.

Community
  • 1
  • 1
Tarang
  • 75,157
  • 39
  • 215
  • 276
  • Meteor.bindEnvironment worked perfectly. I wish it was in the docs, but it's only vaguely mentioned. Thanks! – 0x6A75616E Mar 09 '14 at 20:27
  • Not sure if it's relevant, but a zero second timeout may cause issues. http://stackoverflow.com/questions/9647215/what-is-minimum-millisecond-value-of-settimeout – Brad M Mar 10 '14 at 14:18
3

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

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

Thoms
  • 51
  • 6
0

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);
    } 
  }
})
Julio Marins
  • 10,039
  • 8
  • 48
  • 54