Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

ASP - Send custom errors to error 500 page

When an error occurs in my ASP page scripts the visitor gets set to a customised error 500 page which displays the standard errors properties from Server.GetLastError()

I would like to be able to send through other useful debugging information such as
Request.servervariables("ALL_HTTP") and perhaps a SQL string if available.

The code below is my attempt to pass custom variables to my custom 500.asp page. It redirects ok but it not working right as it nolonger displays the error properties of Server.GetLastError()

Any ideas on how to achieve this?

Thank you. Paul.
<%@Language="VBSCRIPT"%>
<% On Error Resume Next %>
<html>
<head>
<title>Bad Page 1</title>
</head>
<body>
<% Response.Write 1/0 %>
</body>
</html>
<%
If Err.number <> 0 then
 	Response.Clear
	errorString = Request.servervariables("ALL_HTTP")
	response.Redirect("500.asp?customerrors=" & errorString)
End If
%>

Open in new window

0
benners70
Asked:
benners70
  • 3
  • 2
1 Solution
 
Wayne BarronCommented:
See if this is what you are wanting?

500-100.asp

I designed a script several years ago, and lost it in a hard drive crash
That gathered all the information from:
Site Error, computer version, Browser Version/Type
And a slew of other details, and would then send it to you email.
But, unfortunantly, I lost it all :( A sad day indeed....

Anyway
Hopefully this script will help you out some.
Carrzkiss
<%@ language="VBScript" %>
<%
  Option Explicit
 
  Const lngMaxFormBytes = 200
 
  Dim objASPError, blnErrorWritten, strServername, strServerIP, strRemoteIP
  Dim strMethod, lngPos, datNow, strQueryString, strURL
 
  If Response.Buffer Then
    Response.Clear
    Response.Status = "500 Internal Server Error"
    Response.ContentType = "text/html"
    Response.Expires = 0
  End If
 
  Set objASPError = Server.GetLastError
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
 
<html dir=ltr>
 
<head>
<style>
a:link			{font:8pt/11pt verdana; color:FF0000}
a:visited		{font:8pt/11pt verdana; color:#4e4e4e}
</style>
 
<META NAME="ROBOTS" CONTENT="NOINDEX">
 
<title>Fatal Error on UDzone.com</title>
 
<META HTTP-EQUIV="Content-Type" Content="text-html; charset=Windows-1252">
</head>
 
<body bgcolor="#C5EFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
 
<div align="center">
<table width="510" border="0" cellspacing="3" cellpadding="5" style="border: 2px inset #003366" bgcolor="#FFFFFF">
 
  <tr>    
    <td align="center" valign="middle">
	<h1 style="COLOR:FF0000; FONT: bold 20pt/22pt verdana"><!--Problem-->Fatal Error on <%=LCase(Request.ServerVariables("SERVER_NAME")) %></h1>
    </td>
  </tr>
  
  <tr>
    <td colspan="2" align="center">
	<font style="COLOR:000000; FONT: 8pt/11pt verdana">
    There is a problem with the page you are trying to 
    reach and it cannot be displayed.<br><br>
    <b>We apologize for the inconvenience! Please try again in a few minutes.</b><br>
    <h2 style="COLOR: Green; FONT: 13pt/15pt verdana;">A mail is now send to the administrator about this problem.</h2>
    </font></td>
  </tr>
  
  <tr>
    <td colspan="2">
	<font style="COLOR:000000; FONT: 8pt/11pt verdana">
 
	<hr color="#C0C0C0" noshade>
	
	<p>Technical Information (for support personnel)</p>
 
<ul>
<li>Error Type:<br>
<%
  Dim bakCodepage
  Dim strBody
  strBody = "" & vbCRLF
  strBody = strBody & "Technical Information (for support personnel)" & vbCRLF & vbCRLF
  strBody = strBody & "Error Type:" & vbCRLF & "  "
  
  on error resume next
	  bakCodepage = Session.Codepage
	  Session.Codepage = 1252
  on error goto 0
  
  Response.Write Server.HTMLEncode(objASPError.Category)
  strBody = strBody & Server.HTMLEncode(objASPError.Category)
  
  If objASPError.ASPCode > "" Then 
    Response.Write Server.HTMLEncode(", " & objASPError.ASPCode)
    strBody = strBody & Server.HTMLEncode(", " & objASPError.ASPCode)
  end if  
  Response.Write Server.HTMLEncode(" (0x" & Hex(objASPError.Number) & ")" ) & "<br>"
  strBody = strBody & Server.HTMLEncode(" (0x" & Hex(objASPError.Number) & ")" ) & vbCRLF  & "  "
 
  If objASPError.ASPDescription > "" Then 
		Response.Write Server.HTMLEncode(objASPError.ASPDescription) & "<br>"
    strBody = strBody & Server.HTMLEncode(objASPError.ASPDescription) & vbCRLF & "  "
 
  elseIf (objASPError.Description > "") Then 
		 Response.Write Server.HTMLEncode(objASPError.Description) & "<br>" 
     strBody = strBody & Server.HTMLEncode(objASPError.Description) & vbCRLF & "  "
  end if
  
 
  blnErrorWritten = False
 
  ' Only show the Source if it is available and the request is from the same machine as IIS
  If objASPError.Source > "" Then
    strServername = LCase(Request.ServerVariables("SERVER_NAME"))
    strServerIP = Request.ServerVariables("LOCAL_ADDR")
    strRemoteIP =  Request.ServerVariables("REMOTE_ADDR")
    If (strServername = "localhost" Or strServerIP = strRemoteIP) And objASPError.File <> "?" Then
      Response.Write Server.HTMLEncode(objASPError.File)
      strBody = strBody & Server.HTMLEncode(objASPError.File)
      If objASPError.Line > 0 Then 
        Response.Write ", line " & objASPError.Line
        strBody = strBody & ", line " & objASPError.Line
      end if  
      If objASPError.Column > 0 Then 
        Response.Write ", column " & objASPError.Column
        strBody = strBody & ", column " & objASPError.Column
      end if  
      Response.Write "<br>"
      strBody = strBody & vbCRLF & "  "
      Response.Write "<font style=""COLOR:000000; FONT: 8pt/11pt courier new""><b>"
      Response.Write Server.HTMLEncode(objASPError.Source) & "<br>"
      strBody = strBody & Server.HTMLEncode(objASPError.Source) & vbCRLF
      If objASPError.Column > 0 Then 
        Response.Write String((objASPError.Column - 1), "-") & "^<br>"
        strBody = strBody & String((objASPError.Column - 1), "-") & "^" & vbCRLF & "  "
      end if  
      Response.Write "</b></font>"
      blnErrorWritten = True
    End If
  End If
 
  If Not blnErrorWritten And objASPError.File <> "?" Then
    Response.Write "<b>" & Server.HTMLEncode(  objASPError.File)
    strBody = strBody & Server.HTMLEncode(  objASPError.File)
    If objASPError.Line > 0 Then 
      Response.Write Server.HTMLEncode(", line " & objASPError.Line)
      strBody = strBody & Server.HTMLEncode(", line " & objASPError.Line)
    end if  
    If objASPError.Column > 0 Then 
      Response.Write ", column " & objASPError.Column
      strBody = strBody & ", column " & objASPError.Column
    end if  
    Response.Write "</b><br>"
    strBody = strBody & vbCRLF
  End If
%>
</li>
<p>
<li>Browser Type:<br>
<%= Request.ServerVariables("HTTP_USER_AGENT") %>
</li>
<p>
<li>Page:<br>
<%
  strBody = strBody & vbCRLF
  strBody = strBody & "Browser Type:" & vbCRLF
  strBody = strBody & "  " & Request.ServerVariables("HTTP_USER_AGENT") & vbCRLF & vbCRLF
  strBody = strBody & "Page:" & vbCRLF & "  "
 
  strMethod = Request.ServerVariables("REQUEST_METHOD")
 
  Response.Write strMethod & " "
  strBody = strBody & strMethod & " "
  
  If strMethod = "POST" Then
    Response.Write Request.TotalBytes & " bytes to "
    strBody = strBody & Request.TotalBytes & " bytes to "
  End If
 
  Response.Write Request.ServerVariables("SCRIPT_NAME")
  strBody = strBody & Request.ServerVariables("SCRIPT_NAME")
 
  If Request.QueryString <> "" Then
    Response.Write "?" & Request.QueryString
    strBody = strBody & "?" & Request.QueryString
  End If
 
  Response.Write "</li>"
  strBody = strBody & vbCRLF
 
  If strMethod = "POST" Then
    Response.Write "<p><li>POST Data:<br>"
    strBody = strBody & "  POST Data:" & vbCRLF
    If Request.TotalBytes > lngMaxFormBytes Then
       Response.Write Server.HTMLEncode(Left(Request.Form, lngMaxFormBytes)) & " . . ."
       strBody = strBody & Server.HTMLEncode(Left(Request.Form, lngMaxFormBytes)) & " . . ." & vbCRLF & "    "
    Else
      Response.Write Server.HTMLEncode(Request.Form)
      strBody = strBody & Server.HTMLEncode(Request.Form)
    End If
    Response.Write "</li>"
    strBody = strBody & vbCRLF
  End If
 
  Response.Write "<p><li>URL:<br>" 
  strURL =  "http://" & LCase(Request.ServerVariables("SERVER_NAME")) & Request.ServerVariables("SCRIPT_NAME")
  If Request.QueryString <> "" Then
    strURL = strURL & "?" & Request.QueryString
  End If
 
  Response.Write "<A HREF=""" & strURL & """>" & strURL & "</a>"
  Response.Write "</li>"
 
  strBody = strBody & "  " & strURL
  strBody = strBody & vbCRLF
 
%>
<p>
<li>Time:<br>
<%
  datNow = Now()
 
  Response.Write Server.HTMLEncode(FormatDateTime(datNow, 1) & ", " & FormatDateTime(datNow, 3))
  strBody = strBody & vbCRLF & "Time:" & vbCRLF & "  " &  Server.HTMLEncode(FormatDateTime(datNow, 1) & ", " & FormatDateTime(datNow, 3))
  on error resume next
	  Session.Codepage = bakCodepage 
  on error goto 0
  
  strBody = strBody & vbCRLF
 
  Dim objCDO
  Set objCDO = Server.CreateObject("CDONTS.NewMail")
  objCDO.From = "tech-support@pcitdad.com"
  objCDO.To = "webmaster@pcitdad.com"
  objCDO.CC = ""
  objCDO.Subject = "Fatal error on " & LCase(Request.ServerVariables("SERVER_NAME")) 
  objCDO.Body = strBody
  objCDO.Importance = 2
  objCDO.Send()
  Set objCDO = Nothing
 
%>
</li>
<p>
<li>More information:<br>
<a href="mailto:support@yoursite.com">Yoursite Support</a>
</li>
</ul>
    </font></td>
  </tr>
  
</table>
</div>
</body>
</html>

Open in new window

0
 
benners70Author Commented:
Hi Carrzkiss, thanks for your reply.

I wasn't immediately sure what you have provided but I replaced my custom error 500 page with your code and it worked, but I'm not sure it's quite what I was after.

I think key question I have is can I pass variables used on the page with an error through to the IIS error page for display and still get access to all the properties of Server.GetLastError()?

I can of course redirect to another error handling page if  an error is found and pass variables that exist on the page that had the error (see code snippet). But if I do this, I don't seem to be able to access to the properties of Server.GetLastError() unless I go through to the IIS error 500 page.

If Err.number <> 0 then
	if variableName1 <> "" then 
		errorVariables = variableName1 
	end if
	if variableName2 <> "" then 
		errorVariables = errorVariables & variableName2 
	end if
	response.Redirect("anotherPage.asp?errorVariables=" & errorVariables)
        'OR
        'fnSendErrorVariables errorVariables 
        'response.Redirect("myCustomError500IISpage.asp")
end if

Open in new window

0
 
Wayne BarronCommented:
Try this.
http://www.devguru.com/technologies/ASP/quickref/server_getlasterror.html
<%
Dim objErrorInfo
Set objErrorInfo = Server.GetLastError 
 
Response.Write("ASPCode = " & objErrorInfo.ASPCode)
Response.Write("ASPDescription = " & objErrorInfo.ASPDescription)
Response.Write("Category = " & objErrorInfo.Category)
Response.Write("Column = " & objErrorInfo.Column)
Response.Write("Description = " & objErrorInfo.Description)
Response.Write("File = " & objErrorInfo.File)
Response.Write("Line = " & objErrorInfo.Line)
Response.Write("Number = " & objErrorInfo.Number)
Response.Write("Source = " & objErrorInfo.Source)
%> 

Open in new window

0
 
benners70Author Commented:
Hi Carrzkiss

From the info you've given, it seems it's not possible to either:
a) pass variables from a page with an error through to a custom IIS error500 page
b) access Server.GetLastError anywhere other that in the IIS error500  page

In laymans terms for the ISS error500 page to load the page with an error can't load and for a page with an error to resume next and load would mean the  ISS error500 page cannot be accessed.

If I'm incorrect in these conclusions I'd be pleased to here.

However, by accessing various ServerVariables in my custom error 500 page I am able to debug most errors.

Thanks
0
 
Wayne BarronCommented:
to bad we could not do what you really wanted.
So, the [B] is ok, considering that it was not what you was really needing.

Have a good one.
Carrzkiss
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now