This is a cleaned up version for your use -- in my own case, its a bit more complex as I use a logging database to log much of the agent activity I do.
In that case, the script library opens that database and automatically creates a document with fields for database name, agent name, start time, end time, etc. It also writes logging output to that file in any agent using with a debug_print() function used instead of "print" or "msgbox" throughout the agent. My version also handles the functionname as a stack, so the entire stack can be listed rather than just the current function.
So, here's my simplified version for you:
I understand there is an open source version of this as well -- mine has been in continual use since something like 1994 so it predates that open source one just a bit.
-----------------------------
Using the technique below, I get the following advantages:
1. By just including the library, even if I set nothing, I get some level of information about errors.
2. If I remember to set the agent & database name in initialize of the agent, I get more information
3. If I remember to set & clear the functionname on entry and exit of functions and methods, I have complete detail on the location of the error. If not, I've got the name of the most recent one I did remember to set, which isn't great, but its better than nothing.
4. I can event set event monitors on the console to watch for "ERROR!" in the log output and send notifications out.
'Set these in Agent Initialize
'NCT_DBNAME ' ---------------- String. Set in agent Initialize as the name of the database.
'NCT_AGENTNAME ' ------------- String. Set in agent Initialize as the name of the agent or script.
'NCT_ISWEBAGENT ' ------------ Boolean. Set in agent Initialize causes the output to use msgbox instead of print.
'NCT_NORMALTERMINATION ' ----- Boolean. Set in agent Initialize to true, Set in agent Terminate to false
' if set to true when the script library terminate event runs, will flag that the agent didn't complete
' because even if the agent errors out, the script library termination event still fires.
'Ideally, in the initialize as well as any created subs, functions, and object methods, I wrap with code like this:
Sub whatever()
on error goto errorHandler
Dim PrevFunctionName as String
PrevFunctionName = NCT_FUNCTIONNAME
NCT_FUNCTIONNAME = "ThisFunctionName"
:allDone
NCT_FUNCTIONNAME = PrevFunctionName
exit Sub
:errorHandler
NCT_ReportError(erl, error$, "")
resume allDone
End Sub
' ***************************************** SCRIPT LIBRARY *********************************
Declarations
Private NCT_DBNAME = "DatabaseName"
Private NCT_AGENTNAME = "AgentName"
Private NCT_ISWEBAGENT as Boolean
Private NCT_FUNCTIONNAME as String
private NCT_NORMALTERMINATION as boolean
Sub initialize
on error goto errorHandler
' probably not much code here, as it is the initialize section of the script library
:allDone
exit Sub
:errorHandler
NCT_ReportError(erl, error$, "Default Error Handler.")
resume allDone
End Sub
Sub NCT_ReportError( erln as Integer, errortxt as String, byVal AddtlTxt as String)
If NCT_ISWEBAGENT=true then
msgbox("ERROR! " & NCT_DBNAME & " [" & NCT_AGENTNAME & "] " & NCT_FUNCTIONNAME & " line: " & erln & " - " & errortxt & ". " & addtltxt)
Else
print ("ERROR! " & NCT_DBNAME & " [" & NCT_AGENTNAME & "] " & NCT_FUNCTIONNAME & " at line: " & erln & " - " & errortxt & ". " & addtltxt)
End If
End Sub
Sub Termination
close ' closes any open text files
if NCT_NORMALTERMINATION = false then NCT_ReportError("Agent Terminated Abnormally.")
End Sub
' ************************************** END SCRIPT LIBRARY *********************************
Comment Entry |
Please wait while your document is saved.
thank you for your contribution to the community. The main point is DOING some
kind of error logging or handling.
I have just one point: Your library is about error LOGGING only, not error
handling. This would include some "extension" points or settings for at least
some errors, which can be handled in some way - this could be just as simple as
to ignore an "Document not found" error in a function using GetDocumentByUNID
and returning Nothing.
As written on Nathan's blog: using OOP here - with derived classes - could
offer such extension points in your library. As far as I remember, Julian
Robichaux's OpenLog works this way.
Again, the most important part in error handling/logging is: JUST DO IT.
Thomas