Using the %lasterrormsg Variable

Top  Previous  Next

 

The %lasterrormsg variable is initialized to a text description of the last error of the most recently completed script command. This message is the same as what appears in the script log. The corresponding numeric error code is stored in the %lasterror internal variable.

 

Some commands do not change the value of %lasterrormsg when they complete successfully. These commands include DISPLAY, SET and conditional branching commands like IFERROR. This exception allows you to add robust error handling logic to your command scripts.

 

The FTPSETERROR command sets the value of %lasterrormsg to text portion of the value of %ftplastresult (the last response from the remote server.)

 

If you want to use this value to report an error via email it is necessary to save the value in a script file variable so that it doesn't get reset by the results of a subsequent command. In the following example, the value of %lasterrormsg is stored in a variable named err_msg for use later in the body of an email message.

 

RCVFILE "daily_transactions.xml"

IFERROR= $ERROR_SUCCESS GOTO next_step

SET err_num = %lasterror

SET err_msg = %lasterrormsg

SET from = "Robo-FTP Service"

SET email = "[email protected]"

SET subj = "Automated download failed! (do not reply)"

SET body = "The following error was reported: "

SET body = body + "[" + err_num + "] " + err_msg

SET attach = ""

CREATEMAIL from email subj body attach

SENDMAIL "120.33.13.10" "Help Desk" "[email protected]"

GOTO done

:next_step

 

 

Related command(s): IFERROR, FTPSETERROR

See also: %lasterror, Internal Script Variables, Script File Result Codes, Interactive Debugging, Simple Error Handling Example