Error Handling

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
JCUK
Newbie
Posts: 3
Joined: Sun Jan 08, 2012 6:02 pm
Location: UK

Error Handling

Post by JCUK » Thu Jan 19, 2017 3:23 pm

Hi, I'm trying to tidy up a script that runs nightly by getting it to close gracefully in the event of an error. At the moment, the extra code I'm putting in place looks like:

Let>ONERROR=ErrorCleanup
....
....

srt>ErrorCleanup
Let>ONERROR=ErrorInErrorHandler
'Log useful diagnostic information
'clean up (close open windows and files).
Exit
END>ErrorCleanup

srt>ErrorInErrorHandler
'Not a lot can do now!
Exit
END>ErrorInErrorHandler

The part I'm struggling with is the useful diagnostic information. Ideally, something on the nature of the error and the line number that was being executed at the time. I've seen references to "LAST_ERROR" but can't find out much about it.

One additional complication is that the macro contains some VBScript and errors can sometimes occur there.

Any suggestions on useful information to log and how to do so would be very welcome.

Thanks.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Error Handling

Post by Marcus Tettmar » Fri Jan 20, 2017 11:03 am

LAST_ERROR will contain the error message. You can also use LAST_ERROR_LINE to see which line caused the last error. What I do is create my own log file and use the TimeStamp command to log out the error details. At the end of the script we will also often have this log file emailed to someone.

So at the start of the script I would declare the log file:

Let>myLOG=%SCRIPT_DIR%\mylog.txt

And in the error subroutine something like:

TimeStamp>myLOG,%LAST_ERROR% at %LAST_ERROR_LINE%

I might sometimes add a parm to my error routine so I can call it with my own message. This is useful where e.g. I'm checking for a window timeout or something like that and I want to call my error routine directly rather than it being just a response to a script error.

As for VBScript errors you would have to handle those differently. For a scheduled script the important thing is you want things to error silently and carry on rather than leave a message box popped up. So you probably want to use resume next and then check the error number after certain operations. You could have your VBScript function return something if there was an error and then check the return value after VBEval and use this to call your error sub if needed. There's a useful article on VBScript error handling here:

https://technet.microsoft.com/en-us/lib ... 92852.aspx
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

JCUK
Newbie
Posts: 3
Joined: Sun Jan 08, 2012 6:02 pm
Location: UK

Re: Error Handling

Post by JCUK » Fri Jan 20, 2017 1:05 pm

Exactly what I was looking for Marcus.

Thank you for your help.

Joe

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Error Handling

Post by mightycpa » Fri Jan 05, 2018 8:23 pm

Hi Marcus,
I'm running 14.3.1.
Error Handling doesn't completely work for me. I don't want an error dialogue, I just want to trap and capture the error, then exit gracefully so that the entire process doesn't get hung on a modal dialog waiting on a click.

My code:

Code: Select all

Let>IGNOREERRORS=1
Let>ONERROR=MyErrorHandler
...
error happens somewhere in here, lines 10 - 700
...

Label>MyErrorHandler
  Wait>5
  Let>WLN_NOCRLF=0
  WriteLn>%v_disaster%,v_err_wrt,An error has occured. This process will stop.
  WriteLn>%v_disaster%,v_err_wrt,%ini_file%
  WriteLn>%v_disaster%,v_err_wrt,it could be the CSV file
  WriteLn>%v_disaster%,v_err_wrt,working on this field: %v_fieldname%
  WriteLn>%v_disaster%,v_err_wrt,v_data = %v_data% and qfields %v_recctr% %v_colctr% = qfields_%v_recctr%_%v_colctr% and datalen = %v_len%
  WriteLn>%v_disaster%,v_err_wrt,Here are the error details:
  TimeStamp>%v_disaster%,%LAST_ERROR%
  GoTo>TheEnd
The output is this:
An error has occured. This process will stop.
D:\APPS_TO_COMPILE\GLIM/GLIMHDR.INI
it could be the CSV file
working on this field: ' '
v_data = and qfields 3 48 = qfields_3_48 and datalen = 93
Here are the error details:
13:20:52:743 - %LAST_ERROR%
The error description gets cleared from the variable. I've tried it with and without the % chars. How do I fix this?

This answer is mission critical for me, so any help I can get is appreciated.

Thanks
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Error Handling

Post by Marcus Tettmar » Mon Jan 08, 2018 3:47 pm

I wonder if this is something specific to the particular error you are getting. Are you able to share a script which I can run here that would generate the same error and replicate?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Error Handling

Post by mightycpa » Mon Jan 08, 2018 5:11 pm

This script would fail on many levels for you if you tried to run it. Let me see if I can replicate the problem in a script that you will be able to run. In the meantime, here's more information that might spark a thought:

I'm running it from a SQL Server job. The way that works is I've got a job, I've got a batch file and I execute the batch file from the SQL Server job.

I have a process that builds a fixed length text file that I need to send to a mainframe. We've got a lot of these, so rather than hardcode the instructions, I keep them in a separate CSV file. The kind of errors that I'm generating have to do with instructions in the file . This is an example of what the file looks like:

Code: Select all

field,table.column,length,alignment,padding,format,quotes
COUNTRY,897,3,L,N,s,Y
LCODE,"00",2,L,Y,.2d,N
pFILEID,121,3,L,Y,s,Y
FILESEQUENCE,'0',4,R,Y,p,N
TYPEOFLEDGERINDICATORTOLI,L,1,L,Y,s,Y
aDIVISION,16,2,L,Y,s,Y
aMAJOR,107,3,L,Y,s,Y
aMINOR,0112,4,L,Y,s,Y
aSUBMINORMANDATORYINLEADING,0016,4,L,Y,s,Y
LUNIT,FED,6,L,Y,s,Y
FILLER1,' ',28,L,Y,s,N
pLEDGERSOURCE,121,3,L,Y,s,Y
These end up as the SELECT portion of a SQL statement that I execute, and I use these instructions to both build the SQL and also to format the output for the target file. The way I do it is to read this CSV file into an array, and use each element for what it is intended. Sometimes, I will do something stupid, like this:

Code: Select all

aSUBMINORMANDATORYINLEADING,0016,4,L,Y,s,Y
LUNIT,FED,6,L,Y,s,Y
FILLER1,28,L,Y,s,N   <----- here, compare to above and you'll see that I've forgotten to include the ' '  
pLEDGERSOURCE,121,3,L,Y,s,Y
The effect of this omission is to shift each element to a different place in the array. Eventually the script will attempt to get the length of the field, and in this case, my length variable will be set to L instead of 28, because I omitted a field. When it tries to use a length of "L", that's what generates a syntax error.

The upshot of this explanation is that these errors appear to be syntax errors because of the way I'm doing things. Also, they can appear on any of several lines, depending on what the data represents. What I'm trying to capture is which field is causing me the error, because I know my syntax is right, and my problem is more likely "garbage in - garbage out." To do that, I include the aforementioned code. I've since added LAST_ERROR_LINE and that doesn't work either. The line it fails on, and the error and the fieldname (which does print) are invaluable in quickly identifying my error.

I'll post a script you can run that fails the same way if I can make it do so. If such a script doesn't fail, then maybe that's a clue too. I don't know.

Thanks
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts