The question now is how to we "trap" errors? Well, after any ADO call that accesses the database, we will want to put the following lines:
If Err.number <> 0 then TrapError Err.description End If
You will want to put this after all ADO calls that communicate directly with the database.
This includes ConnectionObject.Open, ConnectionObject.Execute,
and RecordsetObject.Open. Now, you may be wondering where the sub TrapError
is defined: we're about to do that. Create a file called ErrorHandler.asp and put it in
your /include or /scripts directory. In ErrorHandler.asp, we will
have the following subs:
TrapError
ProcessErrors
Let's look at the code for ErrorHandler.asp:
<%
Dim strErrorMessage
Dim bolErrors
'Initialize variables
strErrorMessage = "" 'The error messages for tech. support
bolErrors = False 'Have we found any errors yet?
'Now our two subs
sub TrapError(strError)
bolErrors = True 'Egad, we've found an error!
strErrorMessage = strErrorMessage & strError & ", "
end sub
'If there are any errors, this function will email tech. support
sub ProcessErrors()
if bolErrors then
'Send the email
Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.To = "techsupport@mysite.com"
objCDO.From = "techsupport@mysite.com"
objCDO.Subject = "AN ADO ERROR OCCURRED"
objCDO.Body = "At " & Now & " the following errors occurred on " & _
"the page " & Request.ServerVariables("SCRIPT_NAME") & _
": " & _
chr(10) & chr(10) & strErrorMessage
objCDO.Send
Set objCDO = Nothing
'Now, we've got to print out something for the user
Response.Write "There has been a database error. Technical Support " & _
"has already been notified. You will be informed when " & _
"this issue is resolved. Thank you for your patience!"
end if
end sub
That's all there is to it! Of course, you need to have the SMTP piece installed on your webserver.
(See this article for more information on sending email using
CDO.). Now, at the top of all your pages that you want to use the error handling routines, you'll need
to include ErrorHandler.asp like so:
<!--#include virtual="/include/ErrorHandler.asp"-->
(See this article for more information on using
includes.) You also need to make a call to the ProcessErrors sub at the end of
all your ASP scripts which use this Error Trapping. So, the last line in the ASP file would
be <% ProcessErrors %>. It is also imperative that you have On Error Resume Next at the top of all these
pages as well!
Well, hopefully that wasn't too difficult to follow! Error handling allows you to cope with error messages in a more pleasant way for the end user. Furthermore, it helps with debugging. User's accounts of errors are usually quite different than the actual error messages! With this approach, however, you're technical support department will receive the actual error messages! Neat, eh? (Oh yeah, one other benefit. I had implemented a system like this at my first internship. Folks would usually email me after an error occurred, but due to this email notification, I'd be to their office before they finished writing the email to me.)




