How to suppress server method call error?

I want to suppress a server method call error since the error is non-critical and I want my method to continue running.

I’ve tried to create a new Meteor.Error object (see code below) and throw that but that seems not to work at all. I always get an Error 500 whether I provide a custom Meteor.Error object or leave it empty without throwing anything.

How do I suppress the error entirely to not get any error?

server\get_items.js

Meteor.http.get(endpoint, function (error, results) {
        if (error) {
            throw new Meteor.Error("error-fetching-item", "Error while importing item");
        };
        ....
});

client\get_items.js

Meteor.call("myServerMethod", { 
          option_one: "option_one",
          option_two: "option_two",
       }, 
       function (error) {
           if (error) {
               ... // This always fires with an Error 500 
           } else {
               ...
           }
       }
);

You are calling HTTP.get asynchronously so it returns the error after your server method has returned.

Try using the synchronous version in your server code, i.e.,

try {
   let result = Meteor.http.get(endpoint);
} except (error) {
   // Do whatever you want here
}
1 Like

Still, you shouldn’t see an error on the client if you choose to ignore (swallow) the error on the server. For example:

a) meteor create suppress-error

b) meteor add http

c) Add the following to suppress-error.js:

if (Meteor.isServer) {
  Meteor.methods({
    test() {
      HTTP.get('https://somebadurl.garb', (error, results) => {
        console.log(error);
        console.log('still running ...');
      });
    }
  });
}

d) Call the following via your browser console:

Meteor.call('test', function (error) { 
  if (error) { 
    console.log('oh no!'); 
  } else { 
    console.log('all good!') 
  } 
});

You won’t see any error on the client side - your output will look like the attached.

1 Like

as @eldog points out, you should use the synchronous (fibers)-way. In your second example the method returns before the error is thrown, so the client will never get the error.

@hwillson thanks for the step-by-step answer! You were right when you said I shouldn’t be seeing the error if I choose ignore it on the server.

Throwing it back to the client actually stops the execution on the server. The underlying problem that I kept getting an ERROR 500 was totally different from the issue I was asking above and fixing that issue brought back the normal behaviour of ignoring the error.

server\get_items.js

Meteor.http.get(endpoint, function (error, results) {
        if (error) {
            console.log("Error while importing item");
        };
        ....
});

The above doesn’t interrupt the execution of the server method nor does it raise an error in the client.

Thanks for your help!