How to return an error from the Meteor.call method inside another Meteor.call

My meteorite code at some points goes through several Meteor.call methods. If I have an error in the second layer and I want to throw this meteor error back to the client side, how can I do this?

I currently have something like this, but I get very confusing results, and I don’t think I fully understand what happens when I call throw new Meteor.Error(500, e.category_code, e.description);

In client.js

 Meteor.call('firstCall', data, function (error, result) { if(result) { doSomething(); } else{ console.log(error);//just shows 500 } }); 

In server.js

 var Future = Meteor.npmRequire("fibers/future"); function extractFromPromise(promise) { var fut = new Future(); promise.then(function (result) { fut.return(result); }, function (error) { console.log(error); fut.throw(error); }); return fut.wait(); } firstCall: function (data){ try{ Meteor.call('secondCall', data, 'http://testhref.com/test', 'http://testhref2.com/test' function (error, result) { return result; }); } catch(e){ throw new Meteor.Error(500, e.category_code, e.description); } } secondCall: function (data, paymentHref, otherHref){ try{ var associate = extractFromPromise(balanced.get(paymentHref).associate_to_customer(otherHref).debit({ "amount": data.paymentInformation[0].total_amount * 100, "appears_on_statement_as": "Trash Mountain"})); } catch(e){ Collection.update(data.id, { $set: { 'failed.category_code': e.category_code, 'failed.description': e.description } }); throw new Meteor.Error(500, e.category_code, e.description); } } 
+5
source share
2 answers

In your case, the catch in firstCall will not have anything specific for e.category_code and e.description when you click secondCall . This is because in secondCall you pass these two arguments to Meteor.Error , which take error , reason and details as their arguments:

https://github.com/meteor/meteor/blob/devel/packages/meteor/errors.js

To skip this, you need to modify firstCall to use these properties:

 firstCall: function (data){ try{ Meteor.call('secondCall', data, 'http://testhref.com/test', 'http://testhref2.com/test'); } catch(e){ throw new Meteor.Error(500, e.reason, e.details); } } 

I'm not even sure that you need to split it into two modularity calls, as you can just use the usual Javascript functions. But we can discuss it elsewhere.

+2
source

A few things to mention here:

  1. The Async function does not throw exceptions (except that you make them something like synchronization with Meteor._wrapAsync , as I will explain later), they return an error in a different way (as the first argument in the NodeJS callback). This applies to both Meteor.call and your doSomeAsyncThing .
  2. I do not see the benefits of using Meteor.call on the server. Meteor.call designed to call server methods from the client. In this case, you can simply call YourObj.secondCall from firstCall .
  3. Returning anything from within the callback (as you do inside firstCall ) has no effect. You want your asynchronous code to work as a synchronization code, so I suggest using Meteor._wrapAsync , which is very well explained here . So, I would change the server side a bit:
 firstCall: function (data){ try{ return this.secondCall(data); } catch(e){ throw new Meteor.Error(500, e.category_code, e.description); } secondCall: function (data){ try{ return Meteor._wrapAsync(doSomeAsyncThing)(data); } catch(e){ Collection.update(data.id, { $set: { 'failed.category_code': e.category_code, 'failed.description': e.description } }); throw new Meteor.Error(500, e.category_code, e.description); } 

Hope this helps!

0
source

Source: https://habr.com/ru/post/1202441/


All Articles