Using the %lasterror Variable |
Top Previous Next |
The %lasterror variable is initialized to the last error or completion code of the most recently completed script command. The corresponding error text is stored in the %lasterrormsg internal variable.
Some commands do not change the value of %lasterror 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 %lasterror to the numeric 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 %lasterror is stored in a variable named err_num 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: %lasterrormsg, Internal Script Variables, Script File Result Codes, Interactive Debugging, Simple Error Handling Example |