Search code examples
javascriptvalidationtry-catch

Throwing an error from the if block, results into execution of other statements as well


I am writing a code to validate the fields of the form. Hence I have written a function names validateFields to do so. So if all the fields are right the function will return true else false. The validateFields function is invoked in another function called saveForm. The purpose of saveForm function is to save the data only if the validateFields returns true. The saveForm already performs another promise callback within a try block and if the promise fails an error is returned. What I want to achieve is that if validateFields return false then I want to throw an error in saveForm function.

// this function is present in another file say validation.js
function validateForm(){
//some logic to validate which returns true or false;
}
 
//this function is present in another file say saveForm.js, the structure of saveForm function is as follows
function saveForm(){
 var isFormValid = validation.validateForm(); //validateForm function invoked
try{
//some existing logic to send the saved form
}catch(error){
console.log(error)
}

}

Now if isFormValid is false then I don't want to execute the existing logic within the try block and instead throw an error saying that Form Validation failed. I tried doing it in the following way but it failed

function saveForm(){
try{
 var isFormValid = validation.validateForm(); //validateForm function invoked
try{
//some existing logic to send the saved form
}catch(error){
console.log(error)
}catch(error){
console.log("Validation failed error");
}

}
}

I just want the outer catch block to execute but instead, I get the messages from both the catch blocks, so can anyone please help me with how do I place the isValidForm logic such that I get only the validation failed error?


Solution

  • There should be a catch or finally after every try!

    In your logic, the last catch should be outside of the first try, to catch its error, even then there is no if statement to check for isFormValid

    Try this logic:

    function saveForm() {
      var isFormValid = validation.validateForm(); //validateForm function
      try {
        if (!isFormValid) {
          throw "Form is invalid";
        } else {
          try {
            //some existing logic to send the saved form
            console.log("form save logic");
            // throw error if something goes wrong. Only for test purpose
            throw "Error while saving";
          } catch (error) {
            console.log(error);
          }
        }
      } catch (error) {
        console.log(error);
      }
    }

    OR this, which IMO is more readable

    function saveForm() {
      var isFormValid = validation.validateForm(); //validateForm function
      if (isFormValid) {
        try {
          //some existing logic to send the saved form
          console.log("form save logic");
          // throw error if something goes wrong. Only for test purpose
          throw "Error while saving";
        } catch (error) {
          console.log(error);
        }
      } else {
          throw "Form is invalid";
      }
    }
    
    //In Another function place this saveForm() function inside a try/catch block to handle the custom thrown error if form is invalid:
    
    function validateAndSave(){
      try{
         saveForm();
      }catch(e){
         console.error(e);
      }
    }