Simple function that you can use on $http.error() function callbacks to parse any errors into a consistent object that has a consistent structure with a message, isCallbackError and status property.
The function will attempt to deserialize any JSON content in the message and look for a message or Message property to assign to the message parameter.
ww.angular.parseHttpError(args)
Return Value
Error object with this structure:
{
message: "",
isCallbackError: true, // identifier
status: 500, // http error code
}
Parameters
args
The args collection that was passed to the $http.error() function. Typically this means you pass arguments to the function.
Remarks
You can use this function as part of an $http callback:
return $http.get(service.baseUrl + "albums/")
.success(function(data) {
service.albums = data;
})
.error(function() {
var error = ww.angular.parseHttpError(arguments);
// assign to model or whatever else you need to do
vm.error.message = error.message;
vm.error.icon = "warning";
});
and you should always be guaranteed to get a message property back regardless of what type of Http error occurred (including connection errors).
See also:
Class ww.angular.parseHttpError© West Wind Technologies, 1996-2016 • Updated: 03/31/15
Comment or report problem with topic