meteor / meteor

Meteor, the JavaScript App Platform

Home Page:https://meteor.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Meteor 3.0] Promise rejection stops process

henriquealbert opened this issue · comments

// server.js
Meteor.methods({
  shouldFail: async () => {
    Promise.reject(new Error("Oh no!"));
  }
});

// or

Meteor.methods({
  shouldFail: async () => {
    Promise.reject(new Meteor.Error("Oh no!"));
  }
});
// client.js
 Meteor.callAsync("shouldFail");

This shouldn't occur.
On Meteor 2.X this doesn't happen.

Screenshot 2024-01-04 at 16 17 03

As we discussed in #13062, the Meteor 3 main process now runs inside a promise, meaning that if there is an unhandled rejection inside the app, this main process will be killed.

So, it's best to adopt some conventions when dealing with Promises. For example, you can return this Promise.reject:

Meteor.methods({
  shouldFail: async () => {
    return Promise.reject(new Meteor.Error("Oh no!"));
  }
});

Another solution would be to throw an error, like:

Meteor.methods({
  shouldFail: async () => {
     throw new Meteor.Error("Oh no!"));
  }
});

You can also add an unhandledRejection event in your project:

process.on("unhandledRejection", (e) =>
  console.error("Unhandled promise rejection:", e.stack || e)
);

I'm closing this issue, and we can keep discussing it if needed.