There are several ways to handle errors in Angular JS, Some of them are listed bellow:
We can use $exceptionHandler which enables us to catch errors which can occur any where in or application. by default $exceptionHandler prints the error to the console log window, but we can create our own function to handle error.
angular.module('myErrorHandler', [])
.factory('$exceptionHandler', function () {
return function geterrorHandler(exception, cause) {
console.error(exception.stack);
// do some thing
};
});
we can use try can catch block which helps us to catch the exception at client side.
Ex :
function myFunction(){
try {
// do something
}
catch (e) {
// do something
}
}
If we are using $http service for accessing data from server , we have to function success and error and in error function we can put or logic to handle error.
Ex.
$http({method: 'Get',url:'myPage/getData'
}).success(function (data) {
// do some thing
}).error(function (data) {
// do some thing
});
0 Comment(s)