1

I am new to angularjs. I am using angularjs 1.4 version. I am attempting to handle exception which is throwing from Spring controller method. But i could not able to catch the exception in below js below.

May be this is duplicate question, but i had searched solution in SO. But did not work. Please give me suggestion for this. Thanks in advance.

Also throwing exceptions in Spring MVC controller method is correct? or how to say 500 error to front end?

In Angular Controller:

Service.calculate (obj).then(function(response){
    // logic
});

In Angular Service:

this.calculate = function(obj){
     try {
         return $http.post(PATH+'txn/calc',obj).then(function(resp){
                 return resp;
         }); 
     } catch (e) {
         alert(e);
     }
 }; 

Spring controller method :

@RequestMapping(value = "/txn/calc", method = RequestMethod.POST)
public @ResponseBody someBean calculate(@RequestBody SomeBean someBean , HttpServletRequest request) {
    try {
        // exception occurs here
    } catch (Exception e) {
        throw e;
    }
}
1

3 Answers 3

1

try this:

$http.post(url).then(function(response) {
    console.log('post',response)
},
function(data) {
    // Handle error here
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But It is not coming to function(data) block its coming to function(response) block. But it is coming in spring controller catch block.
1

You should add a function as a second parameter for the then function like this:

$http.post(url).then(
function(response) {
  console.log(response);
},
function(data) {
  // Handle error here
  console.log(data);
})

1 Comment

Thanks. But It is not coming to function(data) block. But it is coming in spring controller catch block.
1

You don't must to use Try Catch in your javascript function. You need to use catch function from your promise.

this.calculate = function(obj){
         return $http.post(PATH+'txn/calc',obj).then(function(resp){
             return resp;
          }).catch(function(error){ alert(error) }; 

 }; 

The best pratice is to use a services or factory for return a promise. And you apply your then and catch function in your service.

app.factory('myService',myService);
myService.$inject('$http');
function myService($http){ 
     var services = { 
         calculate:calculate 
     } 
     return services; 

     function calculate(obj){ 
         return $http.post(PATH+'txn/calc',obj) 
     } 
}

After you inject your services or factory in your controller and you call your function from this.

app.controller('myCtrl',myCtrl);
myCtrl.$inject = ['$scope','myService'];
function myCtrl($scope,myService){
    var vm = this;
    vm.calculate = function(obj){
        myService.then(success).catch(error);

        function success(response){...}
        function error(response){...}
     }
}

11 Comments

nevradub thanks, but it says catch invalid identifier in angular contoller
So try to use two callback in your function then. (.then(successCallback, errorCallback))
nevradub. You mean then should have 2 parameters.
Yest, in classic method the first parameter is a success callback and the second parameter is a error callback. Look the official document for more precision if you need. docs.angularjs.org/api/ng/service/$http
Ok thanks nevradub. when i use two callback functions, its going first block i mean success block instead of error block. do you know why.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.