How do I resolve expected PromiseProxyMixin rejections?


#1

I ran into this odd case when I was attempting to use the PromiseProxyMixin to clean up my templates. One of the situations was when I had a component that rendered a list after a search is completed with our API. The searching was debounced because it triggers when the search term changes. I then wrote a cancelation mechanism that would reject the last pending promise before making a new one this.set('promise', newPromise). I started noticing that these rejected promises were being spit out to our logging service because the Ember.onerror were trapping all unhandled rejections even though the Component I had expected that.

Normally in a promise chain you can avoid unhandled errors by making sure you place a .catch() at the end of the chain. However, I don’t know how to do this if the promise is meant to be used in a PromiseProxyMixin.

Here is a JSBin that illustrates the problem with expected rejections in a PromiseProxyMixin.


#2

Well as it turns out at the time of assigning the promise you can use this.catch to prevent the underlying promise to be unhandled and still set the isRejected and reason correctly:

this.set('promise', newPromise);
this.catch(function(err) {
  // Check if we should handle this or blow up
});

Here is an example from the previous code that illustrates this.

An example might look like:

actions: {
  goSearch: function() {
    this.set('promise', cancellablePromise(this.get('query')));
    this.catch(function(err) {
      if (! err instanceof CancellationError) {
        throw err; // Unexpected rejection; should be considered unhandled.
      }
    });
  }
}