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! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations. FREE! Go There Now!
|
|
|
|
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. 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!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |