Created
December 4, 2013 09:29
-
-
Save rafinskipg/7784772 to your computer and use it in GitHub Desktop.
Angular HTTP console, logs all activity
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
(function (angular) { | |
'use strict'; | |
// Intercepting HTTP calls with AngularJS. | |
myApp.config(['$provide', '$httpProvider', function ($provide, $httpProvider ) { | |
// Intercept http calls. | |
$provide.factory('myLoggingInterceptor', ['$q','UtilsSrv', function ($q, UtilsSrv) { | |
return { | |
// On request success | |
request: function (config) { | |
if(config.url.indexOf('views/') == -1){ | |
UtilsSrv.consolaJSON(config, config.method + ' -> '+ config.url); | |
} | |
// Return the config or wrap it in a promise if blank. | |
return config || $q.when(config); | |
}, | |
// On request failure | |
requestError: function (rejection) { | |
UtilsSrv.consolaJSON(rejection, rejection.config.method + ' -> ' + rejection.config.url, 'error'); | |
// Return the promise rejection. | |
return $q.reject(rejection); | |
}, | |
// On response success | |
response: function (response) { | |
if(response.config.url.indexOf('views/') == -1){ | |
UtilsSrv.consolaJSON(response, response.config.method + ' -> ' + response.config.url, 'warn'); | |
} | |
// Return the response or promise. | |
return response || $q.when(response); | |
}, | |
// On response failure | |
responseError: function (rejection) { | |
switch (rejection.status){ | |
case 500: | |
UtilsSrv.consolaJSON( rejection.config.data, rejection.config.method + ' -> ' + rejection.config.url, 'error'); | |
break; | |
} | |
// Return the promise rejection. | |
return $q.reject(rejection); | |
} | |
}; | |
}]); | |
// Add the interceptor to the $httpProvider. | |
$httpProvider.interceptors.push('myLoggingInterceptor'); | |
}]); | |
}(angular)); |
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
(function () { | |
'use strict'; | |
angular.module('myApp') | |
.factory('UtilsSrv', | |
[ function () { | |
//You can disable the loggin of data here, also you can set the level of debug | |
var debugMode = true; | |
var logLevel = 3; | |
// 3-> all , 2 -> warn, 1-> error | |
function consolaJSON(obj, msg, type){ | |
if(debugMode){ | |
type = type ? type : 'info'; | |
msg = msg ? msg : type; | |
switch(type){ | |
case "error": | |
console.error('URL: %o', msg ) | |
console.error(angular.toJson(obj,true)); | |
break; | |
case "warn": | |
if(logLevel > 1){ | |
console.warn('URL: %o', msg ) | |
console.warn(angular.toJson(obj,true)); | |
} | |
break; | |
case "info" : | |
if(logLevel > 2){ | |
console.info('URL: %o', msg ) | |
console.info(angular.toJson(obj,true)); | |
} | |
break; | |
} | |
} | |
} | |
return { | |
consolaJSON : consolaJSON | |
}; | |
}]); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment