Created
July 1, 2015 18:14
-
-
Save tautologistics/0aa28ef03d9b6f385b08 to your computer and use it in GitHub Desktop.
Angular XHR debug logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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