asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Error Checking and Notification System Puts You in Control
By Mark Newlands
Rating: 3.6 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    At some time or another we have all been in the situation where we have picked up the telephone and heard the voice of our user telling us a script we have written is creating an error. Given it is the first time we have heard of it, we ask the user what the error said. More often than not he can't remember, so we have to go through the rigmarole of getting the user to walk us through what he did so the error can be repeated. Only in that way can we get the text of the error and can we get the values (if any) in the form or querystring collections or the session variables that help us track down the error.

    This script removes all these problems and puts you in the driver's seat. The instant a VBScript runtime error occurs, it does a number of things:

    • it stops the execution of the page the error occurred on and invokes the error-handling routine
    • it presents the user with one of two messages stating either that the administrator has been notified that an error has occurred and he need do nothing or that the administrator could not be notified of the error (because the email routine failed for some reason). The page the user sees is customizable by using a custom errors-top.asp and errors-bottom.asp
    • it generates an email and sends it to any number of addresses (administrators) you specify. The email contains:
      • a link to the page the error occurred on
      • the error number, description and source (where available)
      • the names and values in the querystring or form collections
      • the names and values of the session variable

    To use the script, all you need to do is 'include' it in every asp page, ensure that Response.Buffer is set to 'True' on every page it is used on and ensure 'on error resume next' is at the top of every asp page. At every point you want to check for errors (e.g., after each database operation, e.g., sql) simply call the function 'CheckLastError(Err)' where 'Err' is the ASP 2.0 VBScript error object. Personally, I also call the function at the bottom of every page via a standard closing include as a matter of course. Let's see how it is done, and in explaining it I presume the reader has experience scripting with ASP.

    The Code

    ###############Script Starts##################

    <% ' this error function captures vbscript Runtime Errors only.

    Function CheckLastError(objError) 'define a function and pass in the 'Err' object

    '######### Begin handling errors #########
    If objError.Number <> 0 Then 'there is a script error

    'set up variables
    intErrNumber = objError.Number
    strErrDescription = objError.Description
    strErrSource = objError.Source

    strServerPage = "http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("SCRIPT_NAME") ' get the page on which the error occurred
    strCRLF = Chr(13) & Chr(10) 'create the paragraph break for use in the email message to the administrator
    strFromName = "Script Error Function" 'Email from name
    strFromAddress = "scripterrors@yourcompany.com"
    strRemoteHost = "mailhandler.yourcompany.com"
    strSubjectText = "Error Occurred on: " & Request.ServerVariables("SERVER_Name") ' the subject for the email to the administrator
    'set up the first part of the email message
    strBodyText = "The following error has occurred at this URL" _
    & strCRLF _
    & strCRLF _
    & strServerPage _
    & strCRLF _
    & strCRLF _
    & "Details are as follows:" _
    & strCRLF _
    & strCRLF _
    & "#########################################################################" _
    & strCRLF _
    & "PAGE LEVEL SCRIPT ERROR DETAILS:" _
    & strCRLF _
    & strCRLF _
    & "Error Number: " & intErrNumber _
    & strCRLF _
    & "Error Description: " & strErrDescription _
    & strCRLF _
    & "Error Source: " & strErrSource _
    & strCRLF _
    & "#########################################################################" & strCRLF

    Response.Clear ' clear the buffer (to remove all the html from the original page before the error)

    ' now start to create the error message by inserting the html template for the top of the page and the text
    %>

    <!--#include file ="errors-top.asp" -->

    <P>An error occurred in the execution of this ASP page:<BR><BR>
    <strong>&nbsp;&nbsp;&nbsp;&nbsp;<a href=<%=strServerPage%>><%=strServerPage%></a></strong><BR><BR><P>

    <% ' begin displaying the script errors to the user %>

    <P><B>Script / Page Error</B><BR>

    1. Error Number: <%= intErrNumber %><BR>
    2. Error Description: <%=strErrDescription%><BR>
    3. Error Source: <%=strErrSource%><BR></P>

    <%
    '################# 'write the querystring values to the bodytext variable (if any)

    strBodyText = strBodyText & "QUERYSTRING VALUES (if any) FOLLOW: " & strCRLF & strCRLF

    For each x in Request.QueryString 'loop through all the querystring values and write them to the email

    strBodyText = strBodyText & x & " : " & Request.QueryString(x) & strCRLF 'add the name / value pair to the string

    next

    strBodyText = strBodyText & strCRLF & "#########################################################################" & strCRLF 'write out delimter for this section

    '################# 'write the form values to the bodytext variable (if any)

    strBodyText = strBodyText & "FORM VALUES (if any) FOLLOW: " & strCRLF & strCRLF
    For each x in Request.Form' loop through all the form values and write them to the email

    strBodyText = strBodyText & x & " : " & Request.Form(x) & strCRLF 'add the name / value pair to the string

    next

    strBodyText = strBodyText & strCRLF & "#########################################################################" & strCRLF 'write out delimter for this section

    '################# 'write the session variable values to the bodytext variable (if any)
    strBodyText = strBodyText & "SESSION VARIABLE VALUES (if any) FOLLOW: " & strCRLF & strCRLF
    For Each sessitem in Session.Contents ' loop through all the session variable values and write them to the email

    strBodyText = strBodyText & sessitem & " : " & Session.Contents(sessitem) & strCRLF 'add the name / value pair to the string

    Next

    '#########Begin Automated Notification########### ......this routine uses ASP Mail, obviously you can use your preferred mailing routine in place of this if required

    ' create the mail object and assign the necessary properties
    Set theMail = CreateObject("SMTPsvg.Mailer")
    theMail.FromName = strFromName
    theMail.FromAddress = strFromAddress
    theMail.RemoteHost = strRemoteHost
    theMail.AddRecipient "", "steve@epsilis.co.uk"
    theMail.Subject = strSubjectText
    theMail.BodyText = strBodyText

    ' attempt to send the mail and notify the client if it fails - if it doesn't then notify that the webmaster has been notified
    if theMail.SendMail = false then 'the mail was not sent correctly

    if theMail.Response <> "" then 'the error was

    strError = theMail.Response

    else

    strError = "Unknown"

    end if

    strMsg = "We tried to notify support@epsilis.co.uk but a program failure occured. Please <a href=""mailto:support@epsilis.co.uk?subject=Error on this URL: " & strServerPage & """> contact support</a> with following information. <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Reason for failure:&nbsp;&nbsp;" & strError
    Response.Write strMsg

    else 'the mail was sent correctly

    strMsg = "We have notified support@epsilis.co.uk of this error. No further action is required."
    Response.Write strMsg

    end if

    '#########End Automated Notification###########

    'include file which writes out concluding html%>

    <!--#include file = "errors-bottom.asp" -->

    <%' clear the error list and STOP page execution
    Err.Clear
    Response.End

    end if

    End Function%>

    ###############Script Ends##################

    That's all there is to it. Happy Programming.

    About the Author

    Mark Newlands has been working with the Web since 1994 and with ASP since it first came out. He set up shop about 18 months ago as a specialist in NT based Web sites and has developed many Web sites, as well as several e-commerce applications. He has now branched out on his own as an independent contractor and is specializing in transactional Web sites using ASP3.0, MSMQ and RDS.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    CustomError 2.0 for IIS
    When errors occur on a Web site, they should be handled in a way that helps the user to get back on track. Unfortunately, setting up customized error pages in IIS usually requires something many Web developers lack -- access to and familiarity with the Web server's administrative interface. With CustomError for IIS, developers can add error pages, coded by hand or created in their favorite editor, by simply uploading them to a designated directory. No administrator intervention is required.
    [Top]
    Other Articles
    Sep 22, 2004 - Unit Test - Testing with NUnit Framework
    Kamran Qamar introduces unit testing with NUnit and offers some best practices, tips, and tricks.
    [Read This Article]  [Top]
    Aug 10, 2004 - Implementing and Promoting Daily Builds
    Automatic daily builds is a well known software engineering best practice. This article introduces a strategy for implementing and promoting daily builds and offers tips and tricks for preventing and fixing breaks.
    [Read This Article]  [Top]
    Jun 21, 2004 - Using Open Source .NET Tools for Sophisticated Builds
    Building an application can be more than pressing F5. With an increasing number of quality packages being released, developers for the .NET platform now have options to create a very sophisticated build process. Aaron Junod describes a sample build environment and shows how a number of tools can work together to make reliable, predictable, and value-added builds.
    [Read This Article]  [Top]
    Jun 18, 2003 - Online Database Functions Testing Tool
    This short article provides source code for a classic ASP online database functions testing application and shows how to configure and use the tool for either SQL Server or Oracle.
    [Read This Article]  [Top]
    Jan 2, 2003 - Web Application Error Handling in ASP.NET
    One of many improvements ASP.NET brings to the development table is in error handling. Adam Tuliper whips up a simple ASP.NET solution for handling those pesky and unexpected post-production errors.
    [Read This Article]  [Top]
    Sep 10, 2002 - Tracing in .NET and Implementing Your Own Trace Listeners
    Mansoor Ahmed Siddiqui explains debugging and tracing and shows how to create custom trace listeners to help ensure hassle-free development.
    [Read This Article]  [Top]
    Sep 5, 2001 - Firing Events in a Shared Hosting Environment
    Firing events on a Web server is an easy task. However most of the easy solutions require you to have your own dedicated IIS or SQL Server on the Internet to play with, a privilege not shared by many. In this article, Matthew Muller shows you how to get the same functionality in a shared hosting environment.
    [Read This Article]  [Top]
    May 25, 2001 - Avoiding a Type Mismatch Error When Using ByRef with ASP and COM
    Unlike programming inside a complete VB system, when using ByRef with ASP and COM, a complication arises because ASP's VBScript is not typed, but the component's VB is typed. This article will briefly explain how ByRef can be used with ASP and COM.
    [Read This Article]  [Top]
    Apr 18, 2001 - Error Reporting - IIS 5.0
    The script in Mark Newlands' article this week handles how errors are displayed and logged. It can capture all values in use at the time (e.g. form, querystring, session,and application level) and records them if you set a Boolean value to do so - displays custom HTML if required. Sends email, logs to database, and/or text file.
    [Read This Article]  [Top]
    Mar 12, 2001 - Transact-SQL Improves Database Error-Handling
    Transact-SQL provides developers with several database error-handling methods. Use these functions to efficiently handle database errors and add an extra level of data validation. This article discusses the @@ERROR, SP_ADDMESSAGE, and RAISERROR functions and provides examples on how to implement them.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers