Using ASP pages with IIS, you are able to trap the 500.100 error and return a custom made error page to prevent users losing faith in your web site. James will now show you how.
Using Internet Information Server (IIS), you can trap both compilation and run-time errors that occur in your ASP page.
Here's an example file that contains errors - we'll run this page in a minute and see how the errors are trapped.
Test500100.asp
<%@ Language=JavaScript %>
<%
// call a function that doesn't exist...
Hello ( );
// or divide by zero
var a = b / 0;
// or don't finish a statement
Unfinished (
%>
Any of the errors in this file would cause a HTTP 500.100 error, which in turn would send a generic message back to the browser - probably the infamous "This page cannot be displayed".
IIS allows you to override this default behaviour, and specify a file that will be called when an error occurs. You can specify an HTML page or an ASP page. I use an ASP page, so the site will send me an email whenever an error occurs. Don't rely on your users telling you that the site crashed!
The Handle500100.asp File
The error handling file is just a normal ASP file. What makes it useful is the use of the Server.GetLastError() method to inform both the user and I (via email) what went wrong. We'll look at that code in a minute.
Just as when an HTTP 404, File not found error occurs, it is a very good idea to apologise profusely and offer your reader some options.
As you can see, I try to keep the user as happy as possible. The error page looks like any other page on the site, apologises and tries to keep the user from leaving the site in disgust.
The Server.GetLastError Method
Microsoft JScript compilation
Syntax error
/cya/Test500100.asp, line 11
The handler wouldn't be very useful if it didn't tell me quickly what was wrong, so I can fix it. An example of the error I get is shown above, with the code to create it shown below:
(As always, you can get the entire source code by clicking on the icon at the end of the article).
var oASPError = Server.GetLastError ( );
var sError = '<p>' + oASPError.Category;
if ( oASPError.ASPCode > '' )
sError += ', ' + oASPError.ASPCode;
sError += '<br><b>' + oASPError.Description + '</b><br>';
if (oASPError.ASPDescription > '' )
sError += oASPError.ASPDescription + '<br>';
if ( oASPError.File != '?' )
{
sError += '<b>' + oASPError.File;
if ( oASPError.Line > 0 )
sError += ', line ' + oASPError.Line;
if ( oASPError.Column > 0 )
sError += ', column ' + oASPError.Column;
sError += '</b><br>';
}
Fairly self-explanatory I hope, and lifted almost verbatim from IIS's default 500 handler. Hey, it's going to work, right?
Sending the Email
The last step is to email me the error. The code to do that is shown below, using my trusty SendEmail function that supports CDONTS, ASPEmail, ASPMail and JMail.
View or download the real source code using the icon at the bottom of this page.
function Report500100 ( sError )
{
// get the page in error
var sURL = '' + Request.ServerVariables ( "URL" );
// don't send mail while I'm testing on my dev machine..
// or if we're running the test file!
if ( IsDebug ( ) || -1 != sURL.indexOf ( 'Test500' ) )
return;
// make up the message body
var sBody = 'The file "' + sURL + '" generated an Internal Server Error\n\n';
var dateToday = new Date();
sBody += 'Time: "' + dateToday.getHours() + ':' + dateToday.getMinutes() + '".\n';
sBody += sError;
// send the email
SendEmail ( '500.100.Handler', 'BadCode@' + sHostDomain, '', 'Reporting error', sBody );
}
Note that you're not referred to this page by IIS, so you don't use HTTP_REFERER to find the page in error. Instead, use Request.ServerVariables ( "URL" ). You get the original page name, not Handle500100.asp as you might expect.
I don't bother sending email when running on my local server, which is what the IsDebug() function detects. I'm the one generating the errors, so there would be little point!
That's (almost) it. I don't need to describe how to setup IIS to use custom error handling - it's fully described in the IIS help - just browse to your local IISHELP documentation.

| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Articles
More By James Shaw
developerWorks - FREE Tools! |
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
|
|
|
|
Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |