In Meteor.publish
, what is a difference between using this.error
and simply throwing an Meteor.Error
?
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 toMeteor.subscribe
, if any. If error is not aMeteor.Error
, it will be mapped toMeteor.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With