skip to main | skip to sidebar
Showing posts with label Error trapping. Show all posts
Showing posts with label Error trapping. Show all posts

When error occurs while you compile or run an ASP page, IIS generates a 500;100 error and executes a Server.Transfer() method to pass control to the currently defined custom error page. (By default this page is "WinDir/help/iishelp/common/500-100.asp")

When control is passed to the custom error page, the Server.GetLastError() method can be used to obtain detailed information regarding the error that occurred. for more info [Creating Custom ASP Error Pages]

While you can setup a custom ASP error page for some website as described in the MSDN article mentioned before; why not customize the default IIS error 500 page [WinDir/help/iishelp/common/500-100.asp] directly,To present users with a friendly error message and do server-wide error logging and/or send it to IT Email..

This is modification for the 500-100.asp to display an error message and send email to IT:
<%@ language="VBScript" %>
<% Option Explicit

If Response.Buffer Then
  Response.Clear
  Response.Status = "500 Internal Server Error"
  Response.ContentType = "text/html"
  Response.Expires = 0
End If

Call SendError()
%>
<html>
<head>
  <style>
  Body,TD  {FONT-SIZE: 11px; FONT-FAMILY: Tahoma }
  </style>
  <META NAME="ROBOTS" CONTENT="NOINDEX">
  <title>Unexpected Error</title>
  <META HTTP-EQUIV="Content-Type" Content="text-html; charset=Windows-1252">
</head>
<body>
  <h3 align="center">Unexpectd Error</h3>
  An unexpected error has occurred. We apologize for the inconvenience.
  <p>Please try the following:</p>
  <ul>
   <li>Click the <a href="javascript:location.reload()">Refresh</a> button, or try again later.</li>
   <li>Open the home page, and then look for links to the information you want. </li>
  </ul>
</body>
</html>
<%
Sub SendError()
  Dim er : Set er = Server.GetLastError      
  Dim msg : msg = "Error 500 on "& Now &"<br>"
  msg = msg & "category:"& er.Category & "<br>"
  If er.ASPCode>"" Then msg = msg & "aspcode:"& er.ASPCode & "<br>"
  msg = msg & "number:"& hex(er.Number) & "<br>"
  
  If er.ASPDescription > "" Then
    msg = msg & "description:"& er.ASPDescription & "<br>"
  ElseIf er.Description >"" Then
    msg = msg & "description:"& er.Description & "<br>"
  end If
  
  If er.Source > "" Then msg = msg & "source:"& er.Source & "<br>"
  If er.File <> "?" Then
    msg = msg & "file:"& er.File & "<br>"
    If er.Line > 0 Then msg = msg & "line:"& er.Line & "<br>"
    If er.Column > 0 Then msg = msg & "column:"& er.Column & "<br>"
  End If
  
  msg = msg & "USER_AGENT:"& Request.ServerVariables("HTTP_USER_AGENT") & "<br>"
  msg = msg & "REQUEST_METHOD:"& Request.ServerVariables("REQUEST_METHOD") & "<br>"
  msg = msg & "SCRIPT_NAME:"& Request.ServerVariables("SCRIPT_NAME") & "<br>"
  if Request.QueryString>""Then msg = msg & "QueryString:"& Request.QueryString & "<br>"
  if Request.Form>""Then msg = msg & "Post:"& Left(Request.Form,500) & "<br>"
  
  Call SendEmail(IT_Email,IT_Email,"Error 500",msg)
End Sub
%>


* don't forget to disable "Show friendly HTTP error messages" In IE under Tools/Internet Options/Advanced

URL rewriting is usually needed to make a URL for a dynamic web page more presentable for the reader or for search engines optimization.

For example, a regular ASP URL might look like this:
http://www.site.com/Articles/Article.asp?id=20

A more presentable way of rewriting the URL might be:
http://www.site.com/Articles/20.htm

of course, you can write HTML/ASP file for each article, but that would be much of I/O overhead on adding/updating articles and for reading too..

one solution is to use an ISAPI filter but many people want to avoid this for due to either the limitation on the skills, a limitation of the hosting service they use, or just to avoid complexity and potential risk. It also makes the solution less portable.

A much simple solution, is to use 404 custom error page in IIS, here are steps and code:
1- Create "URL-Rewrite.asp", under Articles folder.
2- in IIS , right-click on "Articles" folder > properties > Custom errors > Select 404 and click "Edit Properties" ..
set Message Type : "URL", and set URL : "/Articles/URL-Rewrite.asp"
3- Place this code in "URL-Rewrite.asp"
<% option Explicit

Dim Er,ID
Set Er = Server.GetLastError()
If Not Er Is Nothing Then
  '' For 404: URL will be passed as query string
  ID = GetURLID(Request.QueryString )
  'Http error 400 won't raise a code error
  If ID>0 And Er.Number=0 Then
    Response.Status = 200
    Call DisplayArticle(ID)

  ' error 500 or similar  
  ElseIf Er.Number<>0 Then
    Response.Write "Unexpected error was occured"  

  ' undesired URL  
  Else
    Response.Status = 404
    Response.Write "Page cannot be found"  
  End if  
End If  
Set Er = Nothing

Function GetURLID(URL)
  ' Extract ID using regular expressions
  Dim ID : ID = 0
  Dim reg : Set reg = New RegExp
  reg.Global = False
  reg.IgnoreCase = True
  reg.Pattern = "^404;http://www.site.com/Articles/(\d+).htm$"
  if reg.Test(URL) Then
    ID= reg.Replace(URL ,"$1")
  End If
  Set reg = Nothing
  GetURLID = ID
End Function

Sub DisplayArticle(ID)
  '''' here you will place the real code that read article form database and write it
  Response.Write   "<html>" &_
          "<head>" &_
          "<title>Article "& ID &"</title>" &_
          "</head>" &_
          "<body>" &_
          "Content " & ID &_
          "</body>" &_
          "</html>"
End Sub
%>


now,when requesting the url "http://www.site.com/Articles/20.htm", the IIS doesn't find the file so it transfer execution to the custom error page "http://www.site.com/Articles/URL-Rewrite.asp" and sending "404;http://www.site.com/Articles/20.htm" as query string.
The Transfer happens by calling "server.Transfer" which keeps the existing query string and any form variables available to the page it is transferring to, in our case "URL-Rewrite.asp".

Mike

Mike MoreWeb developer, jQuery plugin author, social media fan and Technology Blogger.
My favorite topics are: jQuery , Javascript , ASP.Net , Twitter , Google..
<connect with="me"> </connect>

Subscribe by email

Enter your email address:

or via RSS