Error handling (ubiquitous)

The vast majority of SyncJS’ functions return a boolean value; if true the function has executed without errors, if false the function has returned an error.

Some functions, though, like the ones returning metadata or directory lists, cannot simply return a boolean value. And, regardless, even for those functions that do, a boolean value only tells you if the function has executed correctly or not, it doesn’t tell you what went wrong if this value is false.

But, SyncJS provides a powerful and ubiquitous way to tell exactly what went wrong, and it can be checked literally anywhere in your scripts: the LastError variable.

Its usage is very simple:

  • as long as no error has occurred, this variable will be null

  • when an error has occurred, this variable will contain the error message

Example:

{
    // Let's enable console feedback, in case we're running this script via the shell
    ConsoleFeedback = true;

    // At this point LastError will be null (no error has ever occurred)
    Log(LastError);

    // Create an SFTP client object, and try connect to a non existent server
    var scli = new SftpClient();
    scli.Host = 'failure:22';
    scli.User = 'none';
    scli.Pass = 'none';
    if (scli.Connect()) {
        scli.Close();
    }

    // Check LastError again, and this time it will contain a connection error
    Log(LastError);
}