Local handling of promise rejections

I’m currently implementing error monitoring in my app using the well-know global error handlers:

Ember.onerror = function (err) {
    trackJs.track(err);
    Ember.Logger.assert(false, err);
};

Ember.RSVP.on('error', function (err) {
    trackJs.track(err);
    Ember.Logger.assert(false, err);
});

That said, when I’m creating or updating a record I’d like to show custom error messages to the user or even swallow errors in specific conditions. For instance:

post.save().then(function () {
  // things went well
}).catch(function (err) {
  notify.error('Your post could not be saved.');
  if (err.response.status === 403) {
    notify.error('You are not allowed to post here.');
  }
  if (err.response.status === 500) {
    // holly shit something went wrong, gotta let that stuff bubble up to the global error handler
    Ember.RSVP.reject(err);
  }
});

It feels like a common need to be able to catch rejections locally, do something with them, and potentially let them bubble up to be managed in a centralized location. Does using Ember.RSVP.reject() makes sense here or should I do something else?

Thanks!

just throw

post.save().then(function () {
  // things went well
}).catch(function (err) {
  notify.error('Your post could not be saved.');
  if (err.response.status === 403) {
    notify.error('You are not allowed to post here.');
  }
    throw err;
});
1 Like

Sometimes the answer is way simpler than I would imagine. Thanks for taking the time to answer my question!

1 Like