0

AngularJS 1.2.1
ngResource 1.2.1

I got the weirdest problem. I'm using tokenWrapper by Andy Joslin (AngularJS: How to send auth token with $resource requests?)

I have a resource defined like that:

.factory('someService', ['$resource', 'api_host', 'TokenHandler',
  function($resource, api_host, TokenHandler) {
    var Resource  = $resource(api_host + 'applicant/:command/:xxx', { xxx: '@xxx', command: '@command' }, {
        'get': { method: 'GET', isArray: false },
        'save': { method: 'POST', isArray: false },
        'create': { method: 'put', isArray: false },
        'message': { method: 'post', isArray: false }
    });

    Resource  = TokenHandler.wrapActions( Resource,
      ["query", "get", "save", "remove", "create", "message"] );

    return Resource;
}])

its wrapped by tokenHandler and token is sent with every request and thats great. the problem is with invoking the error callback.

when using the resource like that

var interview = new someService({ command: 'doSomething', xxx: $scope.xxx});
    interview.$create({}, function(response) {
      console.log(response);
    }, function(e) {
      alert('error');
    });

The Problem
When the resource returns 200 the success function (1st argument) is invoked as it should. When the resource returns something else, the error callback is not invoked.

I've tried to log the tokenHandler and it seems like the handler is massing up the arguments.

    var tokenWrapper = function( resource, action ) {
  // copy original action
  resource['_' + action]  = resource[action];
  // create new action wrapping the original and sending token
  resource[action] = function( data, success, error){
    console.log(success, 'success');
    console.log(error, 'error');

    return resource['_' + action](
      angular.extend({}, data || {}, {token: tokenHandler.get()}),
      success,
      error
    );
  };
};

the result of the console.log prints out the data as success and success as error.
how can I fix that?!

Community
  • 1
  • 1
Orz
  • 585
  • 1
  • 5
  • 26
  • 1
    Your first line `AngularJS 1.2.1 ngResource 1.1.5` irritates me. ngResource should be 1.2.1 as well. – angabriel Dec 01 '13 at 14:40
  • It should be, but I throws so much errors out.. I want to fix this error first and then update the version – Orz Dec 01 '13 at 14:44

1 Answers1

0

angular 1.2.0 changed the way promises are handled. and this look like a promise issue (you are not getting the expected order of suceess``failure```. see this for an example. my guess that insisting on using resource from 1.5 doesn't work we'll with this behavior, so you are not getting the expected data. yes. there are error messages when upgrading. but solving them shouldn't take long.

Community
  • 1
  • 1
alonisser
  • 11,542
  • 21
  • 85
  • 139
  • I've decided to upgrade.. It might take some time but it seems its working much better.. – Orz Dec 03 '13 at 10:20
  • the problem remains.. I've updated the angular-resource but error callback is still not invoked. something must change in the tokenHandler I just dont know what.. – Orz Dec 03 '13 at 11:42