voryx / angular-wamp

An AngularJS library for AutobahnJS (WAMP v2)
MIT License
133 stars 28 forks source link

$wamp.call error promised not chained correctly #20

Closed ValentinH closed 9 years ago

ValentinH commented 9 years ago

I have noticed that if I use this code:

$wamp.call('com.myapp.add2', [2, 3]).then(
      function (res) {
         $scope.add2 = res;
      },
      function(res) {
      //error handler
      }
); 

and I return an error from the server, the error handler is never called.

The problem is related to this line: https://github.com/voryx/angular-wamp/blob/master/src/angular-wamp.js#L311. Indeed, the error part of the Then() is not linked as it should be. The angular doc about $q states that we should use return $q.reject(); in the error block to pass to the next error block. In your case, after the first error block, since we don't call return $q.reject(reason);, the next block is the success one.

Example of chaining:

promiseB = promiseA.then(function(result) {
  // success: do something and resolve promiseB
  //          with the old or a new result
  return result;
}, function(reason) {
  // error: handle the error if possible and
  //        resolve promiseB with newPromiseOrValue,
  //        otherwise forward the rejection to promiseB
  if (canHandle(reason)) {
   // handle the error and recover
   return newPromiseOrValue;
  }
  return $q.reject(reason);
});