Skip to content

Instantly share code, notes, and snippets.

/http.js
Created Dec 16, 2014

Embed
What would you like to do?
'use strict';
angular.module('tl.http', ['tl.lib'])
.config(function($httpProvider) {
$httpProvider.interceptors.push('AccessDeniedInterceptor');
$httpProvider.interceptors.push('CsrfTokenInterceptor');
$httpProvider.interceptors.push('ServerErrorInterceptor');
})
.value('LoginUrl', '/account/login?came_from=')
.factory('CsrfTokenInterceptor', function(tl) {
return {
request: function(cfg) {
cfg.headers['X-CSRFTOKEN'] = tl.csrftoken();
return cfg;
}
};
})
.factory('AccessDeniedInterceptor', function($q, $location, $window, LoginUrl) {
return {
responseError: function(resp) {
if (resp.status === 403) {
$location.path(LoginUrl + $window.escape($location.path()));
} else {
return $q.reject(resp);
}
}
};
})
/** Stub */
.factory('ServerErrorInterceptor', function($q, $log) {
return {
responseError: function(resp) {
if (resp.status === 500) {
$log.error(
'Server error at "' + resp.config.url + '":\n' + resp.data);
}
return $q.reject(resp);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.