• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

ASPError Object Help

Hello,

I've recently be reading up abit on the AspError object (http://www.microsoft.com/windows2000/en/server/iis/htm/asp/vbob1dtg.htm) and (http://www.devguru.com/Technologies/asp/quickref/asperror.html)

I've tried a simple script to try and implement it, but most of the properties that I should have access to through the object seem to result in 0 or null, even though I've forced an error to occur.

Here are my two simple scripts:

Page1 has:

--------------------------
<form method="POST" action="ErrorTest.asp">
 <table style="WIDTH: 100%; HEIGHT: 71px" width="100%" align="left" border="0" cellspacing=6>
     <tbody>
        <tr>
            <td width="100%" align="left">Press "Test" to start an error test.  If you enter an incorrect table name in the text box, an error will occur.
            </td>
          </tr>
        <tr>
            <td width="100%" align="left"> &nbsp
            </td>
          </tr>
        <tr>
            <td width="100%" align="left"><input type="text" class="text" name="tablename" size=20>
            </td>
          </tr>
        <tr>
            <td width="100%" align="left"><input type="submit" value="Test" name="submit">
            </td>
          </tr>
</table>
</form>
--------------------------


Page 2 has (I've taken out the username and password on the database for security reasons):
--------------------------
<%
On Error Resume Next
IF len(trim(Request.Form("tablename")))<=0 THEN
      Response.Redirect("ASPErrorObject.asp")
END IF

Dim objConn, rsSave, sqlstr
Dim objErrorInfo

Set objConn=Server.CreateObject("ADODB.Connection")
objConn.Open "Delcare database would go here"

Set rsSave= Server.CreateObject("ADODB.Recordset")

sqlstr="SELECT * FROM " & Request.Form("tablename")

rsSave.Open sqlstr, objConn
If Err.number <> 0 then
      Set objErrorInfo = Server.GetLastError
      Response.Write("ASPCode = " & objErrorInfo.ASPCode & "<br />")
      Response.Write("ASPDescription = " & objErrorInfo.ASPDescription & "<br />")
      Response.Write("Category = " & objErrorInfo.Category & "<br />")
      Response.Write("Column = " & objErrorInfo.Column & "<br />")
      Response.Write("Description = " & objErrorInfo.Description & "<br />")
      Response.Write("File = " & objErrorInfo.File & "<br />")
      Response.Write("Line = " & objErrorInfo.Line & "<br />")
      Response.Write("Number = " & objErrorInfo.Number & "<br />")
      Response.Write("Source = " & objErrorInfo.Source & "<br />")
ELSE
      Response.Write("no error has occured")
End If

rsSave.Close
Set rsSave = nothing
objConn.CLose
Set objConn = nothing
%>
--------------------------

Any help would be appreciated
0
Sebastion
Asked:
Sebastion
  • 5
  • 4
  • 2
  • +1
1 Solution
 
slamhoundCommented:
It's probably not law but I would suggest doing the "Resume Next" bit just before the code you think will generate the error. Otherwise other commands may generate or clear the error before you get to the bit you are really interested in. I also try to switch the "Resume Next" off as soon as I've finished with it so that I DO get error messages for the rest of the code.
Try something closer to this:

<%
IF len(trim(Request.Form("tablename")))<=0 THEN
     Response.Redirect("ASPErrorObject.asp")
END IF

Dim objConn, rsSave, sqlstr
Dim objErrorInfo

Set objConn=Server.CreateObject("ADODB.Connection")
objConn.Open "Delcare database would go here"

Set rsSave= Server.CreateObject("ADODB.Recordset")

sqlstr="SELECT * FROM " & Request.Form("tablename")

On Error Resume Next
rsSave.Open sqlstr, objConn
If Err.number <> 0 then
     Set objErrorInfo = Server.GetLastError
     Response.Write("ASPCode = " & objErrorInfo.ASPCode & "<br />")
     Response.Write("ASPDescription = " & objErrorInfo.ASPDescription & "<br />")
     Response.Write("Category = " & objErrorInfo.Category & "<br />")
     Response.Write("Column = " & objErrorInfo.Column & "<br />")
     Response.Write("Description = " & objErrorInfo.Description & "<br />")
     Response.Write("File = " & objErrorInfo.File & "<br />")
     Response.Write("Line = " & objErrorInfo.Line & "<br />")
     Response.Write("Number = " & objErrorInfo.Number & "<br />")
     Response.Write("Source = " & objErrorInfo.Source & "<br />")
ELSE
     Response.Write("no error has occured")
End If
On Error goto 0

rsSave.Close
Set rsSave = nothing
objConn.CLose
Set objConn = nothing
%>
0
 
SebastionAuthor Commented:
slamhound,

I tried to move the 'on error resume next' down to your example, and still the results are either all null, or all 0.  

I understand what you mean by having the on error resume next turned 'on' only when I think I would need it and then turned 'off' when it gets to code that wouldn't error out, and I'll try to do that in the future (although I'm abit wary, as the main reason I'm doing this is to ensure that the clients/users do not see any error what-so-ever; eventually, instead of just outputting the error, I plan on sending the info to my via email).
0
 
slamhoundCommented:
In that case, you are doing things correct for an overall error handler. Narrowing it down will only help during debugging.

OK, lets go back to basics. What do you get if you try:

On Error Resume Next
This is a very bad line of code!!
If Err.number <> 0 then
         response.write Err.number
end if
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GoofyDawgCommented:
Have you just tried Err.Description, or some other property? As the Err object is intrinsic to ASP (actually a carryover from VB), it's not really necessary to create a new instance. Personally, I've always used the Err object directly without creating a new instance from GetLastError.

GoofyDawg
0
 
SebastionAuthor Commented:
slamhound,

Alrighty, firstly though I should probably give abit of background (might be useful for you too, GoofyDawg).  I was originally using Err.Number and Err.Description as part of my error handling.  Things worked fine, and on an error, I would get an email telling me of it.  The problem, though, was the err.description wasn't always enough to help me solve the problem (I was also using the Request.ServerVariables("SCRIPT_NAME") to tell me the page, btw).

So, abit of searching around and I came to a page that told me to use Err.LineNumber.  When I tried to use it, however, it stopped by code from working (by that I mean that when an error occured, it skipped over the whole email process and just resumed next without doing anything).  When I took Err.LineNumber out, things worked fine again.

To my knowledge, the only way I can get the line number is from the ASPError object (am I wrong in this?).  Still, if I'm wrong, I think it would be interesting to have access to the ASPError object's properties, as most of them seem useful.

Having the line number (and potentially other things that the ASPError object would provide) would be great for me to solve any problems that might occur (considering my site has alot of pages, some of which are quite large).
0
 
mk_bCommented:
try this

rsSave.Open sqlstr, objConn

If err.number>0 then
response.write "VBScript Errors Occured: " & "<br>"
response.write "Error Number=" & err.number & "<br>"
response.write "Error Descr.=" & err.description & "<br>"
response.write "Help Context=" & err.helpcontext & "<br>"
response.write "help path=" & err.helppath & "<br>"
response.write "Native Error=" & err.nativeerror & "<br>"
response.write "Source=" & err.source & "<br>"
response.write "SQLstate=" & err.sqlstate & "<br><br>"
else
response.write "no problems" & "<p>"
response.write  got_ID & "<p>"
end if
IF objConn.errors.count>0 then
response.write "Database Errors Occcccured" & "<br>"
for counter= 0 to objConn.errors.count
response.write "Error #" & objConn.errors(counter).number & "<p>"
response.write "Error Desc. -> " & objConn.errors(counter).description & "<p>"
next
else
end if


rsSave.Close
Set rsSave = nothing
objConn.CLose
Set objConn = nothing




../mk
0
 
SebastionAuthor Commented:
mk_b,

It doesn't seem to like "err.helppath", "err.nativeerror" and "err.sqlstate" (all three of those do not display for me).

One thing that I think might be an issue as to why ASPError object is always returning 0 and null values, is probably because any errors that occur through opening the database connection probably are not classed as ASP errors.  Instead, they are objConn errors (because ObjConn is the variable I use to open the connection)?

Another thing, is that objErrorInfo.Column (from the ASPError Object) seems to work with objConn errors (correctly stating the column that the faulty code begins), but the rest do not?

I tried to move it off an ObjConn error and to what I believe is an ASPError (to see at least if the code is working).

I added the code:

Reponse.Write("Hello")

Right before the "IF Err.Number <> 0 THEN" statement

Once again, all ASPError object properties are either 0 or null, and the err.number, err.description, err.helpcontext and err.source all have the following values:

Err Number = 424
Err Description = Object required
Err HelpContext = 0
Err Source=Microsoft VBScript runtime error

Looking at the results, I would not be able to determine what cause of this error if I had not created the error manually myself (after changing 'Reponse' to 'Response' the code says there is no errors).

This is why I want to be able to use the ASPError object, to at least get the line number (and possible a better description, if the objErrorInfo.ASPDescription and objErrorInfo.Description are more detailed than Err.Description)
0
 
mk_bCommented:
can i tell you what i do on some projects? i will anyway...

i use the script below and save it as error.asp and then i would setup "custom errors" in the IIS properties for the site...

right click the site and goto properties > Custom Errors > on the 500 error edit properties "message type" URl and then "/error.asp"

when ever and error is hit on iis it will forward to the error page and you can handle the error reporting

please note that i dont display the error but i have it emailed to me as displaying it could be a security risk and expose more information than what is needed to to the users or the site> and im using JMail to send the email if you dont have it you can get it free (http://www.dimac.net/FreeDownloads/dlstart.asp?ProductID=5) or change code to cdo.


<%
   On Error Resume Next
   '--Referencing the error object
   set objError = Server.getLastError()
   strNumber = objError.AspCode
   strSource = objError.Category
   strPage = objError.File
   strDesc = objError.Description
   strCode = Server.HTMLEncode(objError.Source)
   If strCode = "" then strCode = "No code available"
   strLine = ObjError.Line
   strASPDesc = ObjError.ASPDescription
   FilePath = Request.ServerVariables("PATH_TRANSLATED")
     
   '--You get the entire context of the page that had the error.
   '--Review the server variables to see if you would want to store more information.
   strRemoteAddr = Request.ServerVariables("REMOTE_ADDR")
   strRemoteHost = Request.ServerVariables("REMOTE_HOST")
   strLocalAddr = Request.ServerVariables("LOCAL_ADDR")

      Dim mail
      set mail = Server.CreateOBject( "JMail.Message" )
        mail.Logging = true
        mail.silent = true
        mail.From = "markb@sawebdesign.co.za"
        mail.AddRecipient "markb@sawebdesign.co.za"
        mail.Subject = "Error on Orion: "& FilePath
        mail.Body = "" & vbCrLf
        mail.appendText "Error Number:" & strNumber & vbCrLf & vbCrLf
        mail.appendText "Source:" & strSource & vbCrLf & vbCrLf
        mail.appendText "File:" & strPage & vbCrLf & vbCrLf
        mail.appendText "Path:" & FilePath & vbCrLf & vbCrLf
        mail.appendText "Description:" & strDesc + ". " + strASPDesc & vbCrLf & vbCrLf
        mail.appendText "Code:" & strcode & vbCrLf & vbCrLf
        mail.appendText "Line:" & strLine & vbCrLf & vbCrLf
        mail.appendText "Remote Address:" & strRemoteAddr & vbCrLf & vbCrLf
        mail.appendText "Remote Host:" & strRemoteHost & vbCrLf & vbCrLf
        mail.appendText "Local Address:" & strLocalAddr & vbCrLf & vbCrLf
        mail.appendText "User:" & session("Name") & vbCrLf & vbCrLf
        mail.appendText "Email:" & session("UserName") & vbCrLf & vbCrLf
        mail.appendText "" & vbCrLf & vbCrLf & vbCrLf & vbCrLf
        mail.appendText "Formdump:" & vbCrLf
        For ix = 1 to Request.Form.Count
        fieldName = Request.Form.Key(ix)
        fieldValue = Request.Form.Item(ix)
        If fieldName = "strUserPass" Then
        Else
        mail.appendText ""& fieldName & ": " & fieldValue & vbCrLf
        End If
        Next
        mail.appendText "Querydump:" & Request.QueryString() & vbCrLf

        mail.appendText "(This mail was automatically generated)."

        if not mail.Send("127.0.0.1") then
        '-Response.Write "Error"
        end if
      Set mail = Nothing
   
   %>
<br><br>

   <table width="50%" align="center" cellspacing="0" cellpadding="0" border="1">
    <tr>
      <td colspan="2">This error has been logged and will be seen to. Please <a href="javascript:history.go(-1)">click here</a> to return to the previous page.<br><br></td></tr>
   </table>
 
<%  
   '--Kill objects
   set objError = nothing
%>


../mk
0
 
SebastionAuthor Commented:
Hey,

Well, considering that I usually log into my site through command prompt --> ftp, right-clicking is going to be abit difficult :).

Still, I've asked someone at my web hosting company, and they said this:

"Yes this is possible, just let me know the path to your custom error page and the error message you wish to change."

You stated : "when ever and error is hit on iis it will forward to the error page and you can handle the error reporting".  I took this as any error occuring.  So, in order for me to simulate this on my server, I would need to change multiple error entries to redirect to my custom error page?  If so, which ones?
0
 
mk_bCommented:
well there are many error page but if you have a asp or db error it will always be a "500 error" the description would be somthing like "Internal server error" so you only need to change that one to get the error for your scripts.

i am asuming that your are using a windows platform. use the instruction above to set the error and please note that the website needs to be running as an application in iis

see these
http://www.aspfaq.com/show.asp?id=2335
http://www.15seconds.com/issue/980210.htm

http://www.microsoft.com/windows2000/en/server/iis/default.asp?url=/windows2000/en/server/iis/htm/core/iierradd.htm

http://www.microsoft.com/resources/documentation/iis/6/all/proddocs/en-us/cerr_enablingcustom.mspx
http://www.aspfaq.com/show.asp?id=2162

../mk
0
 
SebastionAuthor Commented:
Thanks, that's exactly what I needed.

Also, sorry mk_b, but I accidently left your email address (the one listed in the example) on the page when I did my first couple of tests :(.  I dunno what I was thinking.  You can ignore those :)

Thanks again for the help.
0
 
mk_bCommented:
ok cool i was wondering what thoughts where.. i got confussed for a moment?

../mk
0

Featured Post

Independent Software Vendors: 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!

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now