0

I just went through an angular-fullstack application and i came across this piece of code:

catch( function(err) {
      err = err.data;
      $scope.errors = {};

      // Update validity of form fields that match the mongoose errors
      angular.forEach(err.errors, function(error, field) {
        form[field].$setValidity('mongoose', false);
        $scope.errors[field] = error.message;
      });

I understand the piece of code what it is trying to say but i want to know if suppose an error occur what exactly is passed to function(error, field). i unable to interpret what happens if error occurred. so that i'll able to know what is actually happening in this code

This piece of code is in a controller

Can anyone please explain the whole procedure with example?

1
  • That doesn't catch an exception. It's just the error callback of a promise. The promise seems to be rejected with a HTTP response whose body contains a field named errors, which is a map of field:error. Each error has a message. Commented Jun 17, 2015 at 4:52

1 Answer 1

1

Looks like you're "catching" errors (probably returned from a restful service) and mapping each error in the error array to it's specific field.

The validity of that field is then set to false, which is something held by the angular form.

Finally, there is some sort of binding to $scope.errors that displays each error message that is added to the $scope.errors array.

Looks like pretty simple and typical validation. It has nothing to do with core angular error handling and is simply a way to add validation information to the form/page.

Sign up to request clarification or add additional context in comments.

1 Comment

in my form i added an input field like this <input type="email" name="email" class="form-control" ng-model="user.email" required mongoose-error/>... you mean to say that the above code matches with this field and if mongoose-error occur then it simply return the message

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.