1

I'm running a function, and when I enter data correctly it does exactly what I need it to, but when there is invalid data provided I want to return an error message to the user. As you can see in the pic, the request returns the message I want to display but I cannot access it!

My AngularJS controller:

 vm.avpLogic = function(last, avp_id) {
      console.log("Checking AVP Registration");
      alert("Checking AVP Registration");
      registrationDataFactory.checkAVPReg({ last: last, id: avp_id })
      .then(function(response){
        if(!response) {
          console.log(error);
          alert(error);
        }
          console.log(" frontend RESPONSE ");
          console.log(response);
          vm.avp = response;
          vm.addPlayer(vm.registration, vm.avp)
      })
      .catch(function(response){
      // THE RESPONSE COMES BACK HERE FOR INVALID DATA
        console.log(response.body);  // THROWS ERRORS, see pic
        console.log("An error occurred, but we don't know how to log it.", message);
        alert("FAILED AVP CHECK.  Name and ID must match exactly, and they must be active during the rumble.");
        return error;
      });
    }

I've tried logging error, res, response, message and they all come give me an error. If I remove the console.log lines it pops up an alert with the FAILED AVP CHECK message. I want that to be custom, with the message showing in the response window.

Tracing backwards, my data factory console.logs just fine:

function complete(response) {
    console.log("data factory response SUCCESS!");
    return response;
}

function failed(error) {
    console.log("API error response: ", error);
}

When I look in the console, just I see the error object I could access, but can't seem to do.

The part of my NodeJS function sending the error:

   return res.status(422).send({
    message: 'Missing parameters!  Need name and AVP ID'
  }); 
0

1 Answer 1

1

The factory has problems:

ERRONEOUS

 function failed(error) {
    console.log("API error response: ", error);
}

Better

function failed(error) {
    console.log("API error response: ", error);
    //RE-THROW error
    throw error;
}

When a promise error handler fails to re-throw an error, the function returns undefined. This converts the rejected promise to a fulfilled promise with a value of undefined.

For more information, see

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

1 Comment

AH THANK YOU!!! Someone else wrote the code in Angular, and I was trying all sorts of things. My data factory came from a tutorial years ago and I don't get to look at the code as often as I should. No one ever explained I could throw an error like that. :D . FINALLY an answer.

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.