Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: Error Handling


Message #1 by "Riia" <riia.mattila@m...> on Fri, 23 Nov 2001 12:34:30
Hi



I need some help with error handling. Here's the thing:



We can expect three different kind of errors:

1) VBScript errors

2) program errors from components (mainly unexpected ones)

3) errors due to user actions, from components (e.g. the user tries to 

save an invalid date, the date is checked in a component and an error is 

raised to the ASP-page)



Now, errors of type 1 & 2 should lead to a custom error page (500;100) 

where we'd write them down into the database. Errors of type 3 we'd like 

to handle on the page where they happened, because we want to show the 

form controls with the user input data again with a warning like pls-check-

this-field.



What we were planning is this:



*On "normal" pages the code would look something like this:



' Turn the "error handling" off.

On Error Resume Next



Call obj.MethodName



' See if there was an error.

strErrorText = HandleError(lngLanguageCode)



If strErrorText = vbNullString Then

    blnSaveOk = True

Else

    blnSaveOk = False

End If



' Turn the "error handling" back on.

On Error GoTo 0

---

' Either Inform the user about saving the data successfully or

' show the form controls + error text.

If blnSaveOk = True Then...

---



*The function HandleError (in an include file) would look something like 

this:

Public Function HandleError(ByVal lngLanguageCode)

    If Err <> 0 Then

        Select Case Err.number

            Case '*user error*

                ' Fetch the error text from Lookuptable according to 

                ' error and language codes.

            Case

                ' Redirect the user to the custom error page.

        End Select

    End If



    Err.Clear

End Function



* On the custom error page we'd use ASPError-object to retrieve all the 

necessary information about the error.



As we'd normally use On Error GoTo 0, all the VBScript errors should 

automatically lead to the custom error page.



The problems I've encountered so far: On Error Resume Next on "normal" 

pages seem to "disable" ASPError-object so that I can't use it to retrieve 

information about the error in HandleError function. I could use Err-

object, but it doesn't have the File&Line-properties which are of interest 

to us. I'd need the ASPError-object in the function for to get all the 

info about the error to be sent in a querystring on Response.Redirect as 

on Redirect I wouldn't have the ASPError-object to use on the custom error 

page.



Is it truly so that On Error Resume Next "disables" the ASPError-object 

and is the Redirect-method the best way to go to the custom error page (or 

could I just use On Error GoTo 0 in the function and then raise an error, 

supposed that I had all the error info safe in variables?)?



Hope I explained it all clearly enough. Pls feel free to ask more info. 

Would be really nice if someone knew how to help me.



Thanks in advance!



Riia :o)
Message #2 by "phil griffiths" <pgtips@m...> on Fri, 23 Nov 2001 15:04:31
Its not clear from your post, but are you using IIS5 on Win2K?  If so, 

have you tried calling Server.GetLastError in your error handler to get at 

the ASP error object?



HTH - but maybe its rubbish, I don't use IIS5 its just something i saw in 

MSDN...

Phil

> Hi

> 

> I need some help with error handling. Here's the thing:

> 

> We can expect three different kind of errors:

> 1) VBScript errors

> 2) program errors from components (mainly unexpected ones)

> 3) errors due to user actions, from components (e.g. the user tries to 

> save an invalid date, the date is checked in a component and an error is 

> raised to the ASP-page)

> 

> Now, errors of type 1 & 2 should lead to a custom error page (500;100) 

> where we'd write them down into the database. Errors of type 3 we'd like 

> to handle on the page where they happened, because we want to show the 

> form controls with the user input data again with a warning like pls-

check-

> this-field.

> 

> What we were planning is this:

> 

> *On "normal" pages the code would look something like this:

> 

> ' Turn the "error handling" off.

> On Error Resume Next

> 

> Call obj.MethodName

> 

> ' See if there was an error.

> strErrorText = HandleError(lngLanguageCode)

> 

> If strErrorText = vbNullString Then

>     blnSaveOk = True

> Else

>     blnSaveOk = False

> End If

> 

> ' Turn the "error handling" back on.

> On Error GoTo 0

> ---

> ' Either Inform the user about saving the data successfully or

> ' show the form controls + error text.

> If blnSaveOk = True Then...

> ---

> 

> *The function HandleError (in an include file) would look something like 

> this:

> Public Function HandleError(ByVal lngLanguageCode)

>     If Err <> 0 Then

>         Select Case Err.number

>             Case '*user error*

>                 ' Fetch the error text from Lookuptable according to 

>                 ' error and language codes.

>             Case

>                 ' Redirect the user to the custom error page.

>         End Select

>     End If

> 

>     Err.Clear

> End Function

> 

> * On the custom error page we'd use ASPError-object to retrieve all the 

> necessary information about the error.

> 

> As we'd normally use On Error GoTo 0, all the VBScript errors should 

> automatically lead to the custom error page.

> 

> The problems I've encountered so far: On Error Resume Next on "normal" 

> pages seem to "disable" ASPError-object so that I can't use it to 

retrieve 

> information about the error in HandleError function. I could use Err-

> object, but it doesn't have the File&Line-properties which are of 

interest 

> to us. I'd need the ASPError-object in the function for to get all the 

> info about the error to be sent in a querystring on Response.Redirect as 

> on Redirect I wouldn't have the ASPError-object to use on the custom 

error 

> page.

> 

> Is it truly so that On Error Resume Next "disables" the ASPError-object 

> and is the Redirect-method the best way to go to the custom error page 

(or 

> could I just use On Error GoTo 0 in the function and then raise an 

error, 

> supposed that I had all the error info safe in variables?)?

> 

> Hope I explained it all clearly enough. Pls feel free to ask more info. 

> Would be really nice if someone knew how to help me.

> 

> Thanks in advance!

> 

> Riia :o)
Message #3 by "Riia" <riia.mattila@m...> on Sat, 24 Nov 2001 11:38:16
Sorry, I forgot to mention this in my earlier post. Yes, we're using 

IIS5.0 on Win2K so as far as I know, the ASPError-object should work. And 

yes, I've tried to call Server.GetLastError. It works fine on the custom 

error page when I get there after a VBScript syntax error, for example. In 

the HandleError function I do get the error code from the Err-object but 

ASPError-object seems to be empty.



I tried the following on a test page:



On Error Resume Next



a = 1/0



Set objASPError = Server.GetLastError



Response.Write objASPError.Number & "<br>"

Response.Write objASPError.Category & "<br>"

Response.Write objASPError.Description & "<br>"

Response.Write objASPError.ASPDescription & "<br>"

Response.Write objASPError.File & "<br>"

Response.Write objASPError.Line & "<br>"

Response.Write objASPError.Column



Set objASPError = Nothing



This will simply just print 0 for all long-type properties and empty 

strings for all string-type properties. However, if I comment the line On 

Error Resume Next, it will lead to the custom error page which, having the 

same code as above (without the line a = 1/0 of course), will show the 

properties of the ASPError-object properly.



It's a bit frustrating if there's nothing I can do about it. Is there? 

Phil, anyone?



TIA



Riia :o)







> Its not clear from your post, but are you using IIS5 on Win2K?  If so, 

> have you tried calling Server.GetLastError in your error handler to get 

at 

> the ASP error object?

> 

> HTH - but maybe its rubbish, I don't use IIS5 its just something i saw 

in 

> MSDN...

> Phil

Message #4 by "Ken Schaefer" <ken@a...> on Mon, 26 Nov 2001 16:03:08 +1100
According to Wrox ASP v3 Programmers Reference, you need to use a custom 500

Error page.



The full context of the previous page (including the intrinsic collections

etc) are passed to the custom error page.



Cheers

Ken



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: "Riia" <riia.mattila@m...>

Subject: [asp_web_howto] Re: Error Handling





: Sorry, I forgot to mention this in my earlier post. Yes, we're using

: IIS5.0 on Win2K so as far as I know, the ASPError-object should work. And

: yes, I've tried to call Server.GetLastError. It works fine on the custom

: error page when I get there after a VBScript syntax error, for example. In

: the HandleError function I do get the error code from the Err-object but

: ASPError-object seems to be empty.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Message #5 by "Riia" <riia.mattila@m...> on Mon, 26 Nov 2001 07:55:08
Hmmm, I guess what you're saying is that ASPError-object can only be used 

on a custom 500 error page. Too bad. It would have been a handy way to 

retrieve information about the error, but we'll just need to do without 

it. Thanks anyway!



Riia





> According to Wrox ASP v3 Programmers Reference, you need to use a custom 

500

> Error page.

> 

> The full context of the previous page (including the intrinsic 

collections

> etc) are passed to the custom error page.

> 

> Cheers

> Ken

> 

Message #6 by "George Draper" <gdraper@c...> on Mon, 26 Nov 2001 12:15:53 -0500
This is a MIME message. If you are reading this text, you may want to 

consider changing to a mail reader or gateway that understands how to 

properly handle MIME multipart messages.



--=_8ED3BDA0.B7D6BC23

Content-Type: text/plain; charset=US-ASCII

Content-Transfer-Encoding: quoted-printable



You can log your errors to a text file by adding an error handling 

function that loops through the errors and writes to a file using the 

filesystem object.  First put in the On Error Resume Next statement.  I 

use the following to trap the errors:



If Err.number <> 0 or adoConn.Errors.Count <> 0 Then

   ErHandler

End If



Then I include the following function or a variation thereof:



Function ErHandler



  Dim objErrFile Dim mytextstream

  Dim iLoop Dim sErrDesc

  Dim iErrNum



  Set objErrFile =3D CreateObject("Scripting.FileSystemObject")

  Set mytextstream =3D objErrFile.OpenTextFile("D:\path\Errorlog.log", 8, 

TRUE)



  mytextstream.writeline("START Error log Page: " & Request.ServerVariables

.Item("URL") & " Date: " & Now() & "  Err.Number: " & Err.Number)



 If Err.Number <> 0 Then

    mytextstream.writeline("Err.Number =3D " & Err.Number & " ")

    mytextstream.writeline("Err.Description =3D " & Err.Description & " ")

    mytextstream.writeline("Err.Source =3D " & Err.Source & " ")

    mytextstream.writeline("Err.Line =3D " & Err.Line & " ")

    sErrDesc =3D Err.Description

    iErrNum =3D Err.Number

 End if



 If IsObject(adoConn) Then

   If adoConn.Errors.Count > 0 Then

      For iLoop =3D 0 to adoConn.Errors.Count - 1

         mytextstream.writeline(" ADO error no: " & adoConn.Errors(iLoop).N

umber)

         mytextstream.writeline(" Description: " & adoConn.Errors(iLoop).De

scription)

         mytextstream.writeline(" Source: " & adoConn.Errors(iLoop).Source)



         mytextstream.writeline(" SQLState: " & adoConn.Errors(iLoop).SQLSt

ate)

         mytextstream.writeline(" NativeError: " & adoConn.Errors(iLoop).Na

tiveError)

         sErrDesc =3D adoConn.Errors(iLoop).Description

         iErrNum =3D adoConn.Errors(iLoop).Number

      Next

   End If

   Set adoConn =3D Nothing

 End If



 mytextstream.writeline("END Error log Page: " & Request.ServerVariables.It

em("URL") & " Date: " & Now())



 Response.Clear



 'Now send the user to a page to view and curse at

 Response.Redirect "Error.asp?EN=3D" & Server.UrlEncode(iErrNum) & 

"&ED=3D" & Server.URLEncode(sErrDesc)



 Response.Flush

 Response.End

End Function





This is a bit crude, but it does most of what I'm looking for.  It's 

better than logging to a database because it works even if the error is in 

the database connection.  I developed it for an IIS 4 app, but am using it 

now on IIS 5



Let me know if it works for you (if you try it).



- George



>>> riia.mattila@m... 11/26/01 07:55AM >>>

Hmmm, I guess what you're saying is that ASPError-object can only be 

used

on a custom 500 error page. Too bad. It would have been a handy way to

retrieve information about the error, but we'll just need to do without

it. Thanks anyway!



Riia





> According to Wrox ASP v3 Programmers Reference, you need to use a 

custom

500

> Error page.

>

> The full context of the previous page (including the intrinsic

collections

> etc) are passed to the custom error page.

>

> Cheers

> Ken

>





To unsubscribe send a blank email to leave-asp_web_howto-596362D@p...

om




Message #7 by "Ken Schaefer" <ken@a...> on Tue, 27 Nov 2001 10:57:14 +1100
George,



I see your code is using the ADO Connection's Errors Collection, and the

intrinsic VBScript Err object, but I don't see any use of the ASP intrinsic

ASPError Object, which is what Riia was looking for IIRC...



Am I missing something?



Cheers

Ken



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: "George Draper" <gdraper@c...>

Subject: [asp_web_howto] Re: Error Handling





You can log your errors to a text file by adding an error handling function

that loops through the errors and writes to a file using the filesystem

object.  First put in the On Error Resume Next statement.  I use the

following to trap the errors:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Message #8 by "George Draper" <georgedraper@m...> on Mon, 26 Nov 2001 21:01:21 -0500
Oh yeah.  I should have read the original post first.  I see the dilemma

now.  Looks like the ASPError object doesn't get created unless the server

automatically generates a 500;100 error.  Most errors I see are not 500.  I

wonder if you could test the Err.Number to see if it is 500 and if it is,

then extract the ASPError info?  Just a thought.



- George



----- Original Message -----

From: "Ken Schaefer" <ken@a...>

To: "ASP Web HowTo" <asp_web_howto@p...>

Sent: Monday, November 26, 2001 6:57 PM

Subject: [asp_web_howto] Re: Error Handling





> George,

>

> I see your code is using the ADO Connection's Errors Collection, and the

> intrinsic VBScript Err object, but I don't see any use of the ASP

intrinsic

> ASPError Object, which is what Riia was looking for IIRC...

>

> Am I missing something?

>

> Cheers

> Ken

>

> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> From: "George Draper" <gdraper@c...>

> Subject: [asp_web_howto] Re: Error Handling

>

>

> You can log your errors to a text file by adding an error handling

function

> that loops through the errors and writes to a file using the filesystem

> object.  First put in the On Error Resume Next statement.  I use the

> following to trap the errors:

> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

>

>



georgedraper@m...


$subst('Email.Unsub')

>



Message #9 by "Ken Schaefer" <ken@a...> on Tue, 27 Nov 2001 13:55:29 +1100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: "George Draper" <georgedraper@m...>

Subject: [asp_web_howto] Re: Error Handling





: Oh yeah.  I should have read the original post first.  I see the dilemma

: now.  Looks like the ASPError object doesn't get created unless the server

: automatically generates a 500;100 error.  Most errors I see are not 500.

I

: wonder if you could test the Err.Number to see if it is 500 and if it is,

: then extract the ASPError info?  Just a thought.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



I don't think so.



Err is a VBScript object, and so Err.Number gives you the VBScript error. On

the other hand 500 is a HTTP error number.



Cheers

Ken





Message #10 by "Riia" <riia.mattila@m...> on Tue, 27 Nov 2001 08:45:47
Thanks, you all helped me, more or less, even though there's really 

nothing I can do about the problem. I've come to the conclusion that ASP 

ignores the error entirely when you use On Error Resume Next which is why 

the ASPError-object is empty.



As the problem only occurs when I use my own error handling, I can use 

ASPError-object on my custom error page (500;100) with VBScript syntax 

errors. With errors raised from components, I'll just need to be satisfied 

with Err-object. I can give the file name as a parameter to the function 

and then just use querystring to provide it to the custom error page. I'll 

lose ASPDescription and Line -properties but I'll just have to live with 

it.



We have a db table for program erros from all programs so it's best to 

have all the errors in the same place. I could, however, use fso to write 

down all the database connection erros.



Thanks again!



Riia :o)

  Return to Index