javascript - Check network and handle empty error object -
i using parse.com backend ionic framework app means have monitor network activity. can handle online / offline using guide. however, need more 'app wide' solution checks on each call. i'm getting errors when there no network response
server null
causing following error:
error: null not object (evaluating 'response.error')
my question 2 fold:
is possible create network
factory
/service
can injectparsefactory
check , notify user network insufficient.handle
null
response object in code:parsefactory.provider('programme/').get($stateparams.loadprogramme).success (function(data){ // success... }).error(function(response){ $rootscope.$emit('errorevent', {"message" : "unable load programme. please check connection", "errorobject": response.error} ); });
my factory parse calls below:
.factory('parsefactory',['$http','parse_credentials',function($http,parse_credentials){ var baseurl = 'https://api.parse.com/1/classes/'; return { provider:function(type, queryparam, queryvalue) { console.log(queryvalue); wherequery = {} wherequery[queryparam] = queryvalue; var masterquery = {'userid': masterkey}; return { getall:function(){ return $http.get(geturl(type),getparams(wherequery)); }, getmasters:function(){ return $http.get(geturl(type),getparams(masterquery)); }, get:function(id){ return $http.get(geturl(type)+id,getparams()); }, create:function(data){ return $http.post(geturl(type),data,getparams()); }, edit:function(id,data){ return $http.put(geturl(type)+id,data,getparams()); }, delete:function(id){ return $http.delete(geturl(type)+id,getparams()); } } function geturl(type) { return baseurl+type; } function getparams(user) { return { timeout : 5000, headers:{ 'x-parse-application-id': parse_credentials.app_id, 'x-parse-rest-api-key':parse_credentials.rest_api_key, 'x-parse-session-token': parse.user.current()._sessiontoken, 'content-type':'application/json' }, params: { where: user, // limit: 2, // count: 1 // include: "something" } } } } } }])
use $http interceptor. requests using $http passed through interceptors, using interceptors can apply global handling successful or unsuccessful requests , responses.
for example, interceptor cancels request if network connection not available , emits event when empty response received:
app.factory('networkactivityinterceptor', function($rootscope, $q, networkmonitorservice) { return { 'request': function(config) { var canceller = $q.defer(); config.timeout = canceller.promise; if (!networkmonitorservice.hasnetworkconnection()) { // cancels request if there no network connection canceller.resolve(); } // otherwise, let request go through normal return config; }, 'response': function(response) { // handle null/empty response if (!response.data) { $rootscope.$emit('errorevent', { message: 'unable load...', errorobject: response.error }); } return response; }, 'responseerror': function(response) { // response handling above done in responseerror // handler hit when error http code returned. (example: 404, 500) // depends on server configured return. // note in responseerror handler must return rejected promise // (i.e. return $q.reject(response);) } }; });
substitute networkmonitorservice.hasnetworkconnection()
app's network activity logic.
Comments
Post a Comment