PDA

View Full Version : Custom Error Handling Help Needed


mar0364
02-22-2006, 03:21 PM
This code works great (code 1). What I want to do is put it in an include file and call it that on each page like that. I've tried that the two following ways. Just put the code in an include file and call it from that include file from the page. That does not work. Next I said what if I break into three different functions. 1 for "On Error Resume Next" one for the code that reads the error to the page and the last for the End If. Next I include the file at the top of the page and call the functions within deliminators at the appropriate places on the page. That one stopped on the 2nd function looking for an if. (Code 2) is the function.

CODE 1


<%
On Error Resume Next

Open Recordset
%>

<body>
<% If Err.Number <> 0 Then
Response.Write("

The following Error has occurred:</p>")
Response.Write("Number: " & Err.Number & "
")
Response.Write("Description: " & Err.Description & "
")
Response.Write("Source: " & Err.Source & "
")
Err.Clear
Else %>
<%
Loop through recordset and read to page
%>
</table>
</body>
</html>
<%
End If
Close Recordset
%>


Parts of this are fine as a subProcedure but not a function. I'm just as well turning it into a snippet and including it on the pages I need it for.

davidj
02-22-2006, 03:39 PM
any asp people out there ?

mar0364
02-22-2006, 08:00 PM
Bringing this post to a close

This is how I'm doing my error checking. So what do you think?

Include File
<%

Sub WriteError()
Response.Write("

[b]The following Error has occured</strong</p>")
Response.Write("Number: " & Err.Number & "
")
End Sub

Sub RedirectError(url, message)
url = url & "?err=" & message
Response.Redirect url
End Sub
%>


ASP File being checked for errors.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>


<%
On Error Resume Next
Dim Recordset1
Dim Recordset1_numRows
Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_ch2_STRING
Recordset1.Source = "SELECT * FROM tblSubscribers"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()
Recordset1_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
Recordset1_numRows = Recordset1_numRows + Repeat1__numRows
%>
<%
' check for errors
If Err.Number <> 0 Then
' error occured, redirect user to error page
' passing the error description in the querystring
Call RedirectError("errorpage.asp", Err.Description)
End If
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<% If Err.Number <> 0 Then
WriteError()
Else
%>
<%
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
%>
<%=(Recordset1.Fields.Item("txtFirstName").Value)%>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
Recordset1.MoveNext()
Wend
%>

</body>
</html>
<%
End If
Recordset1.Close()
Set Recordset1 = Nothing
%>


Page that reads error
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim Err
Err = Request.QueryString("Err")
%>
<title>Untitled Document</title>
</head>
<body>
<%
Response.Write("

[b]The following Error has occured</strong</p>")
Response.Write(" " & Err & "
")
%>

</body>
</html>