try-catchsahi

Sahi: Try-catch can't handle 'The parameter passed to xyz was not found on the browser" error?


I run all my scripts through a .suite-file roughly in the following form:

_include("variables.sah");
_include("functions.sah");

$a = 0;
while($a<3) {
    try {
        _navigateTo($loginpage);
        login($user, $password);
        myFunction();
        $a = 3
    }
    catch (e){
        _navigateTo($loginpage);
        login($user, $password);
        //undo changes made by myFunction()
        ...
        $a++;
        if($a<3) {
            _log("Try again");
        }
        else {
            _log("Skip to next script");
        }
    }
}

function myFunction() {
    //do this
    ...
}

Now all this runs perfectly fine, except for one thing: it doesn't repeat when it encounters a missing element which under normal circumstances would abort all scripts. It simply ignores the error and moves on to the next line of the suite. How do I make my script retry up to 2 times before moving on, if I don't know which part (if any) is going to fail and when?


Solution

  • Your code looks fine I guess.

    One thing I can think of is that the exception is thrown in the catch block.

    I made a simple script which works as intended:

    var $errors = 0;
    
    function trySet() {
      try {
        _setValue(_textbox("does not exist"), "");
      } catch ($e) {
        $errors++
        _alert($errors);
      }
    }
    
    
    for (var $i = 0; $i < 3; $i++) {
      trySet();
    }
    

    Better figure out where exactly your script runs into problems and handle them with separate try-catch blocks accordingly. How you handle the exceptions is up to you but I guess it would be something like:

    try {
      login()
    } catch ($e) {
      // login failed, try again
    }
    try {
      myfunction()
    catch($e) {
      revertMyFunction()
      //try again
    }
    

    Maybe define your own exceptions to differently react to errors, have a look at this for more info on custom exceptions: Custom Exceptions in JavaScript

    Regards Wormi