Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
renstangular errors handler improved
Browse files Browse the repository at this point in the history
  • Loading branch information
22Viig committed Apr 6, 2016
1 parent 0047372 commit 2a6ee15
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 93 deletions.
72 changes: 26 additions & 46 deletions dist/angular-restheart.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,9 @@
}

RestangularConfigurer.setErrorInterceptor(function (response, deferred, responseHandler) {
// check if session expired
var te = handleTokenExpiration(response);
var f = handleForbidden(response);
var ne = handleNetworkError(response);
return !(te || f || ne); // if handled --> false
handleTokenExpiration(response);
handleForbidden(response);
handleNetworkError(response);
});

RestangularConfigurer.setRequestInterceptor(function (elem, operation) {
Expand Down Expand Up @@ -188,9 +186,7 @@
if (angular.isFunction(restheart.onTokenExpired)) {
restheart.onTokenExpired($location, $state);
}
return true; // handled
}
return false; // not handled
}

function handleForbidden(response) {
Expand Down Expand Up @@ -225,11 +221,7 @@
}
}
}

return true; // handled
}

return false; // not handled
}

function handleNetworkError(response) {
Expand All @@ -244,10 +236,7 @@
if (angular.isFunction(restheart.onNetworkError)) {
restheart.onNetworkError($location, $state);
}
return true; // handled
}

//return true; // not handled
}
});
}
Expand All @@ -256,8 +245,8 @@
'use strict';

angular
.module('restheart')
.service('RhAuth', ['$base64', '$http', 'localStorageService', 'RhLogic', '$q', 'Rh', 'restheart', RhAuth]);
.module('restheart')
.service('RhAuth', ['$base64', '$http', 'localStorageService', 'RhLogic', '$q', 'Rh', 'restheart', RhAuth]);

function RhAuth($base64, $http, localStorageService, RhLogic, $q, Rh, restheart) {

Expand Down Expand Up @@ -313,7 +302,7 @@
return !(angular.isUndefined(authHeader) || authHeader === null);
};

this.signin = function (id, password, errorCallback) {
this.signin = function (id, password) {
var that = this;
return $q(function (resolve, reject) {
that.clearAuthInfo();
Expand All @@ -322,26 +311,26 @@
nocache: new Date().getTime()
};
RhLogic.one('roles', id)
.get(apiOptions)
.then(function (userRoles) {
var authToken = userRoles.headers('Auth-Token');
if (authToken === null) {
that.clearAuthInfo();
resolve(false);
return;
}
that.saveAuthInfo(id, authToken, userRoles.data.roles);
that.setAuthHeader(id, authToken);
resolve(true);
},
function (response) {
if (response.status === 401) {
resolve(false);
} else {
reject(response);
}

});
.get(apiOptions)
.then(function (userRoles) {
var authToken = userRoles.headers('Auth-Token');
if (authToken === null) {
that.clearAuthInfo();
resolve(false);
return;
}
that.saveAuthInfo(id, authToken, userRoles.data.roles);
that.setAuthHeader(id, authToken);
resolve(true);
},
function (response) {
if (response.status === 401) {
resolve(false);
} else {
reject(response);
}

});
});
};

Expand Down Expand Up @@ -390,11 +379,8 @@
}

RestangularConfigurer.setErrorInterceptor(function (response, deferred, responseHandler) {
// check if session expired
var f = handleUnauthenticated(response);
var ne = handleNetworkError(response);
return !(ne || f); // if handled --> false
return f; // if handled --> false
});

function handleUnauthenticated(response) {
Expand All @@ -409,10 +395,7 @@
if (angular.isFunction(restheart.onUnauthenticated)) {
restheart.onUnauthenticated();
}
return true; // handled
}

//return true; // not handled
}

function handleNetworkError(response) {
Expand All @@ -427,10 +410,7 @@
if (angular.isFunction(restheart.onNetworkError)) {
restheart.onNetworkError($location, $state);
}
return true; // handled
}

//return true; // not handled
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-restheart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* then run the 'gulp build' task to update both bower.json and package.json
* See the README.md file for more information.
*/
var VERSION = '1.2.1';
var VERSION = '1.2.2';

var gulp = require('gulp'),
del = require('del'),
Expand Down
17 changes: 3 additions & 14 deletions services/rh.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@
}

RestangularConfigurer.setErrorInterceptor(function (response, deferred, responseHandler) {
// check if session expired
var te = handleTokenExpiration(response);
var f = handleForbidden(response);
var ne = handleNetworkError(response);
return !(te || f || ne); // if handled --> false
handleTokenExpiration(response);
handleForbidden(response);
handleNetworkError(response);
});

RestangularConfigurer.setRequestInterceptor(function (elem, operation) {
Expand Down Expand Up @@ -71,9 +69,7 @@
if (angular.isFunction(restheart.onTokenExpired)) {
restheart.onTokenExpired($location, $state);
}
return true; // handled
}
return false; // not handled
}

function handleForbidden(response) {
Expand Down Expand Up @@ -108,11 +104,7 @@
}
}
}

return true; // handled
}

return false; // not handled
}

function handleNetworkError(response) {
Expand All @@ -127,10 +119,7 @@
if (angular.isFunction(restheart.onNetworkError)) {
restheart.onNetworkError($location, $state);
}
return true; // handled
}

//return true; // not handled
}
});
}
Expand Down
Loading

0 comments on commit 2a6ee15

Please sign in to comment.