[thelist] ASP Error Handling

Martyn Haigh martyn.haigh at virgincosmetics.com
Fri Dec 27 05:50:00 CST 2002


Hi,

There is a time in every web dev's life when they realise that they really
should think about error handling.  Most put it off as it seems like a very
big and very scary thing, which is exactly what I have done until now - the
day after boxing day when work said they weren't gonna give me the day off.

I decided I should teach myself something about this monster of a task.

So - I've posted what I have got at the moment at the bottom of this post.
The only problem is that it's not very generic, and a bit ugly.  I realise
this is my first attempt, but I was wondering if anyone can see anything
wrong with the code, help me improve the code or point me on my way to
making a kill error handling class or anything inbetween.

Thank you so much!

M

<%
On Error Resume Next

Class CError

'----------------------------------------------------------
' Public Declerations
'----------------------------------------------------------
Public eh_bLogErrors

'----------------------------------------------------------
' Private Declerations
'----------------------------------------------------------
Private eh_FilePath
' File Location
Private eh_LogFile
' File Name
Private eh_adminEmail

'----------------------------------------------------------
' Private Functions
'----------------------------------------------------------
Private Sub Class_Initialize()
' Called when class is initialized
	Dim Err
	eh_bLogErrors		= 1

	eh_FilePath			= Server.MapPath("/")
eh_LogFile			= eh_FilePath & "\" & "errors.log"
eh_adminEmail 		= "mhaigh at virgincosmetics.com"
End Sub

Private Sub Class_Terminate()
eh_bDetailedError	= 0
	eh_bLogErrors		= 0
	eh_adminEmail = ""
End Sub

'----------------------------------------------------------
' Public Functions
'----------------------------------------------------------
Public Function LogErrors(bLog)
	eh_bLogErrors = bLog
' Turn on/off error logging
End Function

Public Function LogError()
	Dim objFSO, objTextFile, columnDelimiter
	columnDelimiter = "	"


	' Create File System Object and open Error Log for appending
	Set objFSO		=
Server.CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile(eh_LogFile, 8, 1)

	' Log the Error
	objTextFile.WriteLine now() & ", " & columnDelimiter & Err.number  &
", " & columnDelimiter &  Err.Description  & ", " & columnDelimiter &
Err.Source  & ", " & columnDelimiter &  request.ServerVariables("URL")

	' Clean up
	objTextFile.Close
	Set objTextFile = Nothing
	Set objFSO		= Nothing
End Function

Public Function DisplayError()
	Dim eh_strStandardError
	Dim insertNewLine, insertTab, key, objNewMail
	insertNewLine = vbcrlf
	insertTab = "	"

	If Err.number <> 0 Then
		If eh_bLogErrors = 1 Then LogError
		eh_strStandardError = ("An error has occured :" &
insertNewLine & _
			insertTab & " Date : " & date() & insertNewLine & _
			insertTab & " Time : " & timevalue(now()) &
insertNewLine & _
			insertTab & " Error Number : " & Err.number &
insertNewLine & _
			insertTab & " Error Description : " &
Err.Description & insertNewLine & _
			insertTab & " Page URL : " &
request.ServerVariables("URL") & insertNewLine & _
			insertTab & " Server Variables : " & insertNewLine)

		for each key in request.ServerVariables
			eh_strStandardError = (eh_strStandardError &
insertTab & insertTab & key & " : " & request.ServerVariables(key) &
insertNewLine)
		next


		response.write (eh_strStandardError)
		Set objNewMail = CreateObject("CDONTS.NewMail")
		call objNewMail.Send (eh_adminEmail,eh_adminEmail,"Website
Error",eh_strStandardError)

        Set objNewMail  = Nothing

        response.write("email sent")


	End If
End Function

End Class
%>
Call the error handler with
<%
Set objError = New CError
objError.DisplayError
Set objError = Nothing
%>


Martyn Haigh

Site Developer



More information about the thelist mailing list