intControl 73 - error handling

Started by mpiaser, March 16, 2014, 07:22:40 AM

Previous topic - Next topic

mpiaser

I have a very complex set of Winbatch scripts and I want to be able to create a general error handling routine.  It looks like IntControl 73 will do what I want

What I'd love to see is an example of an error handler in a UDF so I don't have to fight with the syntax and basic stuff.

JTaylor

Here is what I do...I do a lot of database work in case you wonder about the SQLText variable.

Jim

  IntControl(73, 3, @TRUE, "Error_Handling", 0)



#DefineSubRoutine Error_Handling(err)

  last_error   = err[0]
  err_line     = err[1]
  err_offset   = err[2]
  err_assign   = err[3]
  err_file     = err[4]
  err_txt      = err[5]
  err_info     = err[6]
  err_info     = err_info:@CRLF:@CRLF:SQLText
  err_seg      = err[7]
  err_line_num = err[8]
  err_line_udf = err[9]
  err_line_acc = err[10]
 
  err_text = StrCat(         "Error#:          ",last_error,@CRLF)
  err_text = StrCat(err_text,"Line:            ",err_line, @CRLF)
  err_text = StrCat(err_text,"Error Text:      ",err_txt, @CRLF)
  err_text = StrCat(err_text,"Additional Info: ",err_info, @CRLF, @CRLF)
  err_text = StrCat(err_text,"SQLText:         ",SQLText,  @CRLF)
  err_text = StrCat(err_text,"File:            ",err_file, @CRLF)
  err_text = StrCat(err_text,"Offset:          ",err_offset, @CRLF)
  err_text = StrCat(err_text,"Var. Assignment: ",err_assign, @CRLF)
  err_text = StrCat(err_text,"Current UDF:     ",err_seg, @CRLF)
  err_text = StrCat(err_text,"Current Line#:   ",err_line_udf, @CRLF)
  err_text = StrCat(err_text,"UDF Line#:       ",err_line_num, @CRLF)
  err_text = StrCat(err_text,"Accuracy:        ",err_line_acc)

  If IsDefined(SQLText) Then err_text = err_text:@CRLF:@CRLF:SQLText

  ClipPut(err_text)

  DirChange(DirScript())

  ERRHANDLEFormat=`WWWDLGED,6.1`

  ERRHANDLECaption=`Error Handling`
  ERRHANDLEX=001
  ERRHANDLEY=001
  ERRHANDLEWidth=374
  ERRHANDLEHeight=261
  ERRHANDLENumControls=024
  ERRHANDLEProcedure=`DEFAULT`
  ERRHANDLEFont=`DEFAULT`
  ERRHANDLETextColor=`DEFAULT`
  ERRHANDLEBackground=`DEFAULT,192|192|192`
  ERRHANDLEConfig=0
 
  ERRHANDLE001=`333,005,036,012,PUSHBUTTON,DEFAULT,"E&xit",1,1,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE002=`005,077,044,012,STATICTEXT,DEFAULT,"Error#:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34","0|0|0",DEFAULT`
  ERRHANDLE003=`005,093,044,012,STATICTEXT,DEFAULT,"Line:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE004=`005,131,044,012,STATICTEXT,DEFAULT,"Error Text:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE005=`005,169,044,012,STATICTEXT,DEFAULT,"Additional Info:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE006=`005,035,044,012,STATICTEXT,DEFAULT,"File:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE007=`103,077,024,012,STATICTEXT,DEFAULT,"Offset:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34","0|0|0",DEFAULT`
  ERRHANDLE008=`197,063,044,012,STATICTEXT,DEFAULT,"Var. Assign",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE009=`005,049,044,012,STATICTEXT,DEFAULT,"Current UDF:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE010=`005,063,044,012,STATICTEXT,DEFAULT,"Current Line#:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE011=`103,063,044,012,STATICTEXT,DEFAULT,"UDF Line#:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE012=`197,077,044,012,STATICTEXT,DEFAULT,"Accuracy:",DEFAULT,2,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE013=`057,077,040,012,VARYTEXT,last_error,"last_error",DEFAULT,13,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE014=`057,093,312,034,MULTILINEBOX,err_line,"err_line",DEFAULT,13,0,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE015=`057,131,312,034,MULTILINEBOX,err_txt,"err_txt",DEFAULT,22,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE016=`057,169,312,076,MULTILINEBOX,err_info,"err_info",DEFAULT,23,DEFAULT,"Microsoft Sans Serif|6656|40|34","0|0|0",DEFAULT`
  ERRHANDLE017=`057,035,312,012,VARYTEXT,err_file,"err_file",DEFAULT,13,DEFAULT,"Microsoft Sans Serif|6656|70|34","0|0|0",DEFAULT`
  ERRHANDLE018=`129,077,040,012,VARYTEXT,err_offset,"err_offset",DEFAULT,13,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE019=`243,063,126,012,VARYTEXT,err_assign,"err_assign",DEFAULT,13,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE020=`057,049,312,012,VARYTEXT,err_seg,"err_seg",DEFAULT,13,DEFAULT,"Microsoft Sans Serif|6656|70|34","0|0|0",DEFAULT`
  ERRHANDLE021=`057,063,040,012,VARYTEXT,err_line_num,"err_line_num",DEFAULT,13,DEFAULT,"Microsoft Sans Serif|6656|1070|34","0|0|0",DEFAULT`
  ERRHANDLE022=`151,063,040,012,VARYTEXT,err_line_udf,"err_line_udf",DEFAULT,13,DEFAULT,"Microsoft Sans Serif|6656|1070|34","0|0|0",DEFAULT`
  ERRHANDLE023=`243,077,126,012,VARYTEXT,err_line_acc,"err_line_acc",DEFAULT,13,DEFAULT,"Microsoft Sans Serif|6656|40|34",DEFAULT,DEFAULT`
  ERRHANDLE024=`005,005,304,026,STATICTEXT,DEFAULT,"You have encountered the following error.  This text has also been placed on the Windows Clipboard so you can easily paste it into an e-mail if needed.",DEFAULT,24,DEFAULT,"Microsoft Sans Serif|8192|40|34","0|0|0",DEFAULT`
 
  ButtonPushed=Dialog("ERRHANDLE")

  wberrorarray[11] = 1
  Return 1

#EndSubRoutine

stanl

Here is mine. (older version)


Code (Winbatch) Select

;Generic Error Handler
#DefineSubroutine ErrorProcessing(deleteIni,showerr,logfile,Err_Array)
If Vartype(Err_Array) ==256
   WbError = Err_Array[0]
   wberrorhandlerline = Err_Array[1] 
   wberrorhandleroffset = Err_Array[2]   
   wberrorhandlerassignment = Err_Array[3] 
   wberrorhandlerfile = Err_Array[4]
   wberrortextstring = Err_Array[5] 
   wberroradditionalinfo = Err_Array[6] 
   wberrorinsegment = Err_Array[7]   
Else
   WbError = LastError()
Endif
WbTextcode = WbError
If WbError==1668||WbError==2669||WbError==3670
   WbError = ItemExtract(1,IntControl(34,-1,0,0,0),":")
   WbTextcode = -1
EndIf
WbErrorString = IntControl(34,WbTextcode,0,0,0)
WbErrorDateTime = TimeYmdHms()
If deleteIni
   WbErrorFile = StrCat(ShortCutDir( 'AppData', 0, 0 ),'\WinBatch\Settings\')
   If ! DirExist(WbErrorFile) Then WbErrorFile = DirWindows(0)
   WbErrorFile = StrCat(WbErrorFile,"WWWBATCH.INI")
   FileDelete(WbErrorFile)
   IniWritePvt(WbErrorDateTime,"CurrentScript",WbErrorHandlerFile      ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"ErrorValue"   ,WbError                 ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"ErrorString"  ,WbErrorString           ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"ScriptLine"   ,WbErrorHandlerLine      ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"ScriptOffset" ,WbErrorHandlerOffset    ,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"VarAssignment",WbErrorHandlerAssignment,WbErrorFile)
   IniWritePvt(WbErrorDateTime,"VarInSegment" ,WbErrorInSegment,WbErrorFile)
   IniWritePvt("","","",WbErrorFile)
Endif 
WbErrorMsgText = StrCat(WbErrorDateTime,@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"Current Script: ",WbErrorHandlerFile,@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"Error# [",WbError,"]",@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"Error Text: ",wberrortextstring,@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"[Extended Information] ",wberroradditionalinfo,@CRLF,@CRLF)
WbErrorMsgText = StrCat(WbErrorMsgText,"On Line:",@CRLF,WbErrorHandlerLine,@CRLF)
;WbErrorMsgText = StrCat(WbErrorMsgText,"Offset: ",WbErrorHandlerOffset,@CRLF)
If (WbErrorHandlerAssignment>"") Then %WbErrorHandlerAssignment% = "UNKNOWN"
WbErrorMsgText = StrCat(WbErrorMsgText,"Assignment/Variable: ",WbErrorHandlerAssignment,@CRLF)
If (WbErrorInSegment>"") Then WbErrorMsgText = StrCat(WbErrorMsgText,"In UDF/UDS: ",WbErrorInSegment,@CRLF)
If logfile
   cSep = StrCat(StrFill("=",50),@CRLF)
   cLogFile = StrCat(dirscript(),"log.err")
   If ! FileExist(cLogFile) Then FilePut(cLogFile,StrCat("Error Log",@CRLF,cSep))
   FilePut(cLogFile,StrCat(FileGet(cLogFile),WbErrorMsgText,cSep))
   display(2,"An Error Occured",StrCat("written to ",cLogFile))
Else
   If showerr
      WbErrorMsgText = StrCat(WbErrorMsgText,"[THIS ERROR NOT WRITTEN TO LOG FILE]",@CRLF)
      Message("An Error Was Encountered",WbErrorMsgText)
   Endif
Endif
Return(1)
#EndSubroutine


mpiaser

thanks for the replies - they have been very helpful.

Now the next fun thing during error recovery.  If I catch an error, I want to do one of two things:

1) Reboot the computer - which will cause the script to be restarted.  The script recognizes that it is in restart mode and does some special processing.
2) Terminate the winbatch process and relaunch a particular script.

I know how to do #1 but i don't know if I can do #2.

RAK

you can terminate with an exit but before you do - use the shellexecute command to restart your exe and send parameter that will start the script you require.

myexe = exename.exe
shellexecute(myexe,'restartFunc', '', 0, "")
exit

;; in same exe - (early in code just after udf's are loaded)
if '%param1%' == 'restartFunc' then goto myFunc   ; or if '%param1%' == 'restartFunc' then myfunc ()

>>
..you may also use the param as the function name i.e.
if strlen('%param1%') then goto '%param1%'

You can also send multiple paramaters - spaces create separate param's...

shellexecute(myexe,'restartFunc set1 set2', '', 0, "")
p1 = '%param1%'
p2 = '%param2%'
p3 = '%param3%'



Deana

Please see the following tutorial regarding trapping errors in WinBatch. It contains lots of sample code:
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+Tutorials+Trap~Errors.txt

As previously stated, you can simply ShellExecute a second instance of your script then immediately exit. This will terminate the original winbatch process and relaunch a new instance.
Deana F.
Technical Support
Wilson WindowWare Inc.