I have a server api that returns a json string like {status: ok/error, errtext: ..., payload: ...}
. When status !== 'ok'
I would like my program to take the error handling method, the equivalent of $q.reject
. I am using http transform to harmonize some of my data structures.
My code:
var p = $http({
url: url,
timeout: cancel.cancel,
method: 'GET',
headers: addl_headers,
transformResponse: _appendTransform($http.defaults.transformResponse, function (data, headers, status) {
return _doTransform(data, headers, status);
})
});
p.then(function(goodrespObj){...}, function(badnessObj){...});
And
function _appendTransform(defaults, transform) {
defaults = angular.isArray(defaults) ? defaults : [defaults];
return defaults.concat(transform);
}
function _doTransform(data, headers, status) {
if (data.status !== 'OK') {
console.error("bad errtext", data.status, "$http status", status);
(X) ????? something smart that will trigger HTTP ERROR path ?????
}
// ...
return f(data); // this is received as "goodrespObj.data" above
}
If at the point marked (X) if I return the error text, it comes back verbatim to "good" function. How do I indicate a server error, or somehow cause the $q.reject(resp)
path in transformResponse
?
Looking at the code for transformResponse()
I can see that user function has no way to touch response.status
so is there another way?
I am using angular with ionicframework.
edit: removed my previous comment - see the accepted answer.