Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Meteor, what is difference between this.error and throw new Meteor.Error in Meteor.publish?

Tags:

meteor

In Meteor.publish, what is a difference between using this.error and simply throwing an Meteor.Error?

like image 877
Mitar Avatar asked Mar 19 '13 22:03

Mitar


2 Answers

this.error is only available inside the publish method. Per the docs:

Stops this client's subscription, triggering a call on the client to the onError callback passed to Meteor.subscribe, if any. If error is not a Meteor.Error, it will be mapped to Meteor.Error(500, "Internal server error").

Throwing a Meteor.Error would not stop the client's subscription, it would just terminate execution and raise the exception. So if you want to ensure Meteor will clean up after you and allow you to handle the error on the client when something unexpected happens, it's recommended to use this.error rather than throwing your own inside the publish method.

like image 129
Rahul Avatar answered Nov 15 '22 12:11

Rahul


It seems they are the same. In the source code:

try {
  var res = self._handler.apply(self, EJSON.clone(self._params));
} catch (e) {
  self.error(e);
  return;
}

So if there is an exception thrown, error is called anyway. error also stops the subscription.

like image 36
Mitar Avatar answered Nov 15 '22 10:11

Mitar