Friday, 8 August 2008

ArtenSUITE Part 10: Errors and Message Handling

A Development Journal
Part 10: Errors and Message Handling

Errors and Message Handling is an important part of your application. I believe it is necessary to create a framework or standard way of dealing with and displaying and logging errors and messages within your application.

General Messages

I have four kinds of messages that I can display to the user, these are INFORMATION, WARNING, STOP and ERROR. Each error message is handled by a method, the explanation for the message is sent as a parameter to the message method. This is shown below via code and then message image:

mCodeBaseStd.sDisplayInformationMessage("You Are Entering a Secure Module, All Actions Will be Logged ...")

The method mCodeBaseStd.sDisplayInformationMessage looks like this:

dim popup as New MessageDialog
dim result as MessageDialogButton
popup.icon = MessageDialog.GraphicNote
popup.Message = mProgramInfo.pProgramName + ": " + "INFORMATION:"
popup.Explanation = strMessage
result = popup.ShowModal

A icon appropriate to an information message is used and the module name is shown before the word ‘INFORMATION’. All Information messages will be displayed identically, the same goes for the other types of message. This gives a clean, consistent look to your application.

Error Messages

Error messages are handled differently. They are displayed to the user in the same way but there is more to them.

  1. All My Error Messages have a Message Number

  2. Error Message Numbers between 1000 and 2000 are classed as Priority 1, and this is shown to the user

  3. Any Error Description or Number retrieved from the system or database is passed to my Error Display Method

  4. An appropriate Icon is used to depict an error

  5. All Errors are written out to a text file with date and time and module information

  6. All Errors are handled by their own unique method, within the module mCodeBaseErr

When the error occurs the error method is called as such:

mCodebaseErr.sCannotConnectToDB("Could Not Connect to Oracle, The Database is Unreachable ... / Error Code: "+str(pDBOracle.Errorcode))

mCodebaseErr.sCannotConnectToDB looks like this:

dim intErrorNumber as Integer = 1001
dim strErrorMessage as String = "Cannot Connect to Database:"
dim strErrorMessageDisplay as String = strErrorMessage + EndOfLine + strMessage
dim strErrorMessageLog as String = strErrorMessage + " " + strMessage

and mCodeBaseStd.sDisplayAndLogErrorMessage looks like this:

dim popup as New MessageDialog
dim result as MessageDialogButton
popup.icon = MessageDialog.GraphicCaution
popup.Message = mProgramInfo.pProgramName + ": " + "ERROR DETECTED:"
if intErrorNumber >=1000 and intErrorNumber <=1999 then
__popup.Explanation = "Number: " + str(intErrorNumber) +" (Priority 1 Error: Contact Support)" + EndOfLine+ strErrorMessageDisplay
__popup.Explanation = "Number: " + str(intErrorNumber) + EndOfLine+ strErrorMessageDisplay
end if
result = popup.ShowModal

with the final line of the method calling another method which outputs the error to the logfile. This method is shown below:

Dim f as FolderItem
Dim t as TextOutputStream
dim strDate as String = mCodeBaseDB.fSQLFetchSystemDateTime
'Open File Stream
f = GetFolderItem(mCodeBaseGV.pErrorLogFileName)
t = f.AppendToTextFile
'Write to the File
t.WriteLine "<ErrorDate>" + strDate + "</ErrorDate>"
t.WriteLine "<ErrorNumber>" + str(intErrorNumber) + "</ErrorNumber>"
t.WriteLine "<ErrorMessage>" + strErrorMessage + "</ErrorMessage>"
'Close the File

It may seem that my procedure for dealing with errors is complicated. In use it is actually quite simple. I pay a lot of respect to errors and so each one is given their own number and method within mCodeBaseErr, this is the method that is called when the error occurs. The call to output the error to the screen and subsequently the log file is handled automatically, so once I had setup this structure it has been easy for me to build solid handling of errors into my application.

"Anything that has real and lasting value is always a gift from within." - Franz Kafka
Honest Expert Independent Technology Advice for Business

No comments: