Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

meteor

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?

like image 935
0x6A75616E Avatar asked Mar 09 '14 13:03

0x6A75616E


2 Answers

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.

like image 80
Tarang Avatar answered Oct 10 '22 06:10

Tarang


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

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

like image 34
Thoms Avatar answered Oct 10 '22 05:10

Thoms