Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling $q.reject in http interceptor doesn't resolve to calling reject handler in DS.findAll.then #335

Closed
cah-ricksuggs opened this issue Jun 11, 2015 · 0 comments
Assignees
Labels

Comments

@cah-ricksuggs
Copy link

I'm building an http interceptor to check responses for error messages from an API, but don't want to use http status codes, because they always result in messy error message in the browser console. There seems to be an issue in js-data-angular which is making the $.reject not call the errorCallback.

Given an http interceptor which should check for errors in a response object, and call $q.reject(response) if any errors are found in the response:

(function () {
  'use strict';
  angular.module('dash.factories')
    .factory('dashClientHttpInterceptor', ['$rootScope', '$q', function ($rootScope, $q) {
      return {
        responseError: function (rejection) {
          return $q.reject(rejection);
        },
        response: function (response) {
          if(response.data && response.data.error) {
            // *** calling $q.reject to pass through to error callback
            return $q.reject(response);
          }
          return response;
        }
      };
    }]);
}());

When calling DS.findAll(...).then(successCallback, errorCallback, notifyCallback) such as:

$stateProvider
        .state('foo', {
          url: '/list/:date',
          params: {
            date: moment().format('YYYY-MM-DD')
          },
          templateUrl: 'views/foos/main.tmpl.html',
          controller: 'FoosCtrl',
          title: 'Foo List',
          resolve: {
            appointments: ['Foo', '$stateParams', function (Foo, $stateParams) {
              return Foo.findAll().then(
                  function (foos) {
                    return foos;
                  },
                  // *** error callback should be executed
                  function (err) {
                    console.log('Error encountered fetching foos: ' + err);
                    return 'fetchError';
                  }
              );
            }]
          }
        })

Then js-data continues parsing the response as the resource and and throws an exception with an error message in the console such as:

Error: foo.inject: "attrs" must contain the property specified by "idAttribute"!
    at DS._inject (http://localhost:9005/bower_components/js-data/dist/js-data.js:3817:20)
    at DS.inject (http://localhost:9005/bower_components/js-data/dist/js-data.js:3977:23)
    at DS.store.(anonymous function) [as inject] (http://localhost:9005/bower_components/js-data-angular/dist/js-data-angular.js:299:33)
    at Defaults._utils.default.forEach.def.(anonymous function) [as inject] (http://localhost:9005/bower_components/js-data/dist/js-data.js:3378:30)
    at DS.processResults (http://localhost:9005/bower_components/js-data/dist/js-data.js:4441:30)
    at http://localhost:9005/bower_components/js-data/dist/js-data.js:4544:61
    at processQueue (http://localhost:9005/bower_components/angular/angular.js:13248:27)
    at http://localhost:9005/bower_components/angular/angular.js:13264:27
    at Scope.$get.Scope.$eval (http://localhost:9005/bower_components/angular/angular.js:14466:28)
    at Scope.$get.Scope.$digest (http://localhost:9005/bower_components/angular/angular.js:14282:31)

And the reject handler is called because of the error thrown above instead of getting the message from the API such as:

Error encountered fetching foos: Error: foo.inject: "attrs" must contain the property specified by "idAttribute"!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants