Skip to content

Instantly share code, notes, and snippets.

@tautologistics
Created July 1, 2015 18:14
Show Gist options
  • Save tautologistics/0aa28ef03d9b6f385b08 to your computer and use it in GitHub Desktop.
Save tautologistics/0aa28ef03d9b6f385b08 to your computer and use it in GitHub Desktop.
Angular XHR debug logger
$httpProvider.interceptors.push([
'$q', '$rootScope', '$location',
function ( $q, $rootScope, $location ) {
var formatRequest = function (request) {
return JSON.parse(JSON.stringify({
method: request.method,
url: request.url,
headers: request.headers,
data: request.data
}));
};
var formatResponse = function (response) {
return JSON.parse(JSON.stringify({
status: {
code: response.status,
msg: response.statusText
},
headers: response.headers(),
headers: response.headers(),
data: response.data
}));
};
return {
response: function (response) {
if ($location.search().xhrdebug === '1') {
console.log('DEBUG', $location.search());
console.log('DEBUG REQUEST', formatRequest(response.config));
console.log('DEBUG RESPONSE', formatResponse(response));
console.log('DEBUG RESPONSE X', response);
}
return response;
},
responseError: function (response) {
if ($location.search().xhrdebug === '1') {
console.log('DEBUG', $location.search());
console.log('DEBUG REQUEST ERROR', formatRequest(response.config));
console.log('DEBUG RESPONSE ERROR', formatResponse(response));
console.log('DEBUG RESPONSE X ERROR', response);
}
return $q.reject(response);
}
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment