简体   繁体   中英

Is Meteor.Error changing when thrown to client?

I have this:

Meteor.methods({
  'foo'() {
    try{
      ...//some http.get
    } catch (e) {
      console.log(e); //-> { [Error: ETIMEDOUT] code: 'ETIMEDOUT', connect: true } 
      if(e.code === 'ETIMEDOUT') { throw e; }
    }
  }
});

So now i am on client:

Meteor.call('foo', function(error, result) {
  if(error){
    if(error.code === 'ETIMEDOUT') {
      //this block is never reached.. why?
    }
  }
}

But it seems the error.code is not the same as on server (It seems like it's been changed to Internal Server Error). Why is this? And more important, how can i get my original (in this case timeout) error?

from the manual :

When you have an error that doesn't need to be reported to the client, but is internal to the server, throw a regular JavaScript error object. This will be reported to the client as a totally opaque internal server error with no details.

That is what you're seeing. Instead:

When the server was not able to complete the user's desired action because of a known condition, you should throw a descriptive Meteor.Error object to the client. Meteor.Error takes three arguments: error, reason, and details.

so you might do something like this:

if (e.code === 'ETIMEDOUT') {
  let userMessage = 'The remote call timed out.';
  let detail = `${userMessage}: ${JSON.stringify(e)}`;
  console.error(detail);
  throw new Meteor.Error('remote-call-timed-out', userMessage, detail);
}

The first argument, the "error" (i call it a "code"), is something you can program against on the client to take a specific action or internationalize the user message. that's what we do in our system. (and if the code is not found, we show the userMessage). the detail gets written to the server log and put into the browser console.log.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM