how to catch exceptions if any exception occurs in $http call

how to catch exceptions if any exception occurs in http call(at server-side)?first of all Is it correct way to send http post request?what is the difference between then and success in http post request.


var search = angular.module('search', []);

   var req = {"field": 1};

 hboSearchApp.factory('productFactory', function($http,$q){
 
                      var defer = $q.defer();
                    $http.post('searchController',req).then(function(data){
                            defer.resolve(data);
                      });
                                
                                return {
                                                getResponse : function () {
                                                      return defer.promise;
                                                  }
                                          }
                                
            }).controller('productsCtrl', function($http,$scope, productFactory) {
                  
                //This will be called when the page loads
                        
                var promise= productFactory.getResponse();
                        
                promise.then(function(resp)
                {
                              
                  $scope.productList = resp.data;
                              
                });            
});//end of productsCtrl
LVL 20
chaitu chaituAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BigRatCommented:
First "success" is a deprecated method of the $http service and should be replaced by "then"

https://docs.angularjs.org/api/ng/service/$http

Secondly the way you want to defer and catch errors is nicely explained by Peterbe.com, look particularly at the last boxed bit of code headed with "Here's one such possible solution" and the comment by Tomas Lycken.

Personally I use the $http service directly, having no need to defer things, with .success and .error for the cases when I want to report an error, and .then when I silently ignore them. But I'll have to change  in the future.

http://www.peterbe.com/plog/promises-with-$http
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Angular

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.