angularjs - How to handle Restangular .getList() response that's not an array -
the api i'm working returns 200 string error message when client isn't authenticated. if call:
var restsvc = restangular.service('message'); var deferred = restsvc.getlist()
when authenticated array of objects -- if call when not authenticated string "the caller not authenticated."
the problem i'm not able handle error because restangular throws error , doesn't resolve or fail deferred because it's expecting array:
error: response getlist should array , not object or else
how can handle error , response if it's not array?
one thing can add response interceptor test data before returned. if conforms specific format attach response data. example.
restangularconfig.addresponseinterceptor(function(response, operation) { if (operation === 'getlist') { // test data structure here. } return response; });
if own api designing highly recommend changing format consistent every request. example.
if request good.
{ "status": 200, "content": [{},{}...] }
if request bad.
{ "status": 401, //or ever error "content": [] }
Comments
Post a Comment