Home arrow ASP arrow Trapping HTTP 500.100 - Internal Server Error
ASP

Trapping HTTP 500.100 - Internal Server Error


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.

Author Info:
By: James Shaw
Rating: 4 stars4 stars4 stars4 stars4 stars / 16
April 14, 2003

print this article
SEARCH DEVARTICLES

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.

All ASP Tutorials
More By James Shaw


blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials