I need some info on the error handling techniques.
Actually, I tried using the try…catch but I am facing some issue.
Say, The script is run through the cli. I have some function which have the all the required steps in try block and a userdefined Log_error function in the catch block.
Now, these scripts at somepoint of time will be sheduled. Hence, my requiremnt is I want the person debugging the scripts to have more details in the log. The details like what has caused this failure , which line has the failure occured at. etc…can you please provide me some info on how this can be acheived.
----> Added —> For ex the log file gives the error as “Exception close_icon Image Not Found On Screen”-------
How can this info be sent to particular file??
Another concern is, because of the try…catch block, if there is an error in middle of the script, the function exection switches to the catch block and ends the execution. Is there a way, for the script to run other instructions too in the scripts before actually exiting from the script.
When you catch an exception using a try…catch block, the value that is caught will appear to be a single text value if you “put” it. But it’s actually an exception object (a property list) that contains a lot of information about the error. Here’s a little script that illustrates this (copy this into a new script and run it):
catch error -- this will catch an exception object
put error -- this only shows the exception name!
put "- - - - -"
repeat with each key of error's keys -- show everything
put key & ": " & error's (key) & return
put "- - - - -"
-- now display the interesting parts
put error's ScriptError
put the last item of error's CallStack into troubleMaker
put "The problem occurred at line " & troubleMaker's line \
& " of handler " & troubleMaker's handler \
& " in script " & troubleMaker's ScriptObjectID
throw "Problem","Something went terribly wrong!"
The interesting part of this script is the catch block. First it just directly displays the error that it caught, which doesn’t tell you much. Then it displays all of the information contained in the exception object. You’ll see that the exception includes a CallStack list that contains a lot of detail about exactly where the error occurred. The last few lines of the script show how you might extract some of this information for reporting purposes.
[quote=“redstone_doubts”]----> Added —> For ex the log file gives the error as “Exception close_icon Image Not Found On Screen”-------
How can this info be sent to particular file??[/quote]
Writing information into a file is very easy in SenseTalk. Here’s a command you could use in a catch block to append an error message to the end of a log file:
put the date & the time && error's ScriptError \
& return after file "/path/to/my/logfile"
When an exception is thrown, control is immediately transferred to the catch block if there is one. If you want to run other commands after an error, they will need to either be in the catch block, or following it.
Thanks a lot. This second code snippet does serve my purpose, except for one issue that value of “the line the error is found” is always displayed at line 3…I verfied for few other scripts, but still found the same string being displayed.
However, I wanted to know what could be wrong in my script. Please find below some details:
Instead of logging the error at that particluar instance, my intention was to copy all the contents of the sample.suite/results/2006XXXX_XXXXXXX.XXX/LogFile.txt. Hence for this I tried a very crude method of getting the folder name by manpulating some TIME commands and copying the LogFile.txt from the folder.
THe isssue I noticed is, the value from my function always resulted in 20060206_040716.091. The bold value is always same and never matching the value in the results folder. (i.e basically say the excution of a script takes around 8 secs, the value is not reflected. This I verified by calling the Time funtion at the start of the script and end of the script. Note however the eggplant status shows proper time like Execution Time 0:00:08 "
The function I tried to get the tiemstamp for the folder was:
Funtion Time (now)
put the year into yy
put the month into mm
if mm < 10 then
put 0 & mm into mmm
put the day into dd
if dd < 10 then
put 0 & dd into ddd
put the time into tt
replace every occurrence of ":" with empty in tt
replace every occurrence of "PM" with empty in tt
replace every occurrence of " " with empty in tt
put the seconds into ss
divide ss by 10000
put trunc(ss) into sss
divide sss by 1000
put yy & mmm & ddd & "_" & tt & sss into folder
Here, I used the value of the “folder” variable to get the Logfile.txt and copy the contents to a file at my desired location using the commands below:
put file "sample.suite/Results/timeformat" & folder & "/LogFile.txt" into sourceText
open file "/Error/append.txt" for appending
repeat with each line of sourceText
write it to file "/Error/append.txt"
Keep in mind that the line number reported in the exception or by the callStack() function will be the line number within the handler, not within the script. This appears to all be working properly in Eggplant 2.22. If you see something that you believe is incorrect, please submit a bug report (from the Help menu).
[quote=“redstone_doubts”]However, I wanted to know what could be wrong in my script. Please find below some details:
Instead of logging the error at that particluar instance, my intention was to copy all the contents of the sample.suite/results/2006XXXX_XXXXXXX.XXX/LogFile.txt. Hence for this I tried a very crude method of getting the folder name by manpulating some TIME commands and copying the LogFile.txt from the folder.[/quote]
First, let me give you the easy solution for what you’re trying to accomplish. Then I’ll analyze your script to help you understand what went wrong.
The name of the log file for the current run can be obtained using the ScriptResults() function. This function gives a lot of information about every run of a script. To get just the log file name for the current run, the command would be:
put the logFile of the last item of scriptresults() into logFileName
The simplest way to copy a file on the Eggplant machine is with the “copy file” command. So, to copy the current log file, you could use a command like this:
copy file logFileName to file "/tmp/copyOfLogFile"
If you copy the current log file, however, be aware that it is still being created while the script is running, and the final result (success or failure) won’t be written into the log until the script ends. So you may want to write an external controller script that will run your script using the RunWithNewResults command. This will allow the controller script to find out whether the script failed or succeeded and then copy the log file or do other reporting as appropriate.
Now, let’s take a look at the specific issues you ran into in your script:
The problem here is a confusion between two very similarly-named functions in SenseTalk: the seconds; and the second. The seconds returns the number of seconds since the beginning of the millennium (a large number, currently around 160934369). The second (without the ‘s’ at the end) returns the number of the current second within the current minute (a number from 0 to 59).
First, I’d like to suggest that you change the name of your function to something other than “time”, as this is the name of a built-in function. SenseTalk allows you to override built-in commands and functions if you wish, which can sometimes be very useful. But it’s best to avoid this unless you have a specific reason to.
The next thing I notice is that you’re only putting something into mmm and ddd if the month or the day are less than 10. This code will fail in a few days (on Feb. 10) or in October (for the month). SenseTalk has a property called the numberFormat that can provide the leading zero for you when needed. So, here’s a simpler version of this function:
-- set numberformat to ensure at least two digits, with leading zero:
set the numberFormat to "00.###"
return the year & the month & the day & "_" & the hour & the minute & the second + (the millisecond / 1000)
Note that this function still won’t do what you really wanted, however, since the name it comes up with won’t match the name of the log file (which was created slightly before the script began running).