Well I would think the connection would only display because your probably using it as a variable thats doing the "failing".
In classic ASP, connections strings are usually set globally.. in Application scope or as an include for your data provider scripts..
Generally when your script fails, IIS will generate a error code of 500. Other status codes like 404, and 403 deal with page access problems.. like it doesnt exist or you dont have security rights to browse the page..
So what I generally do is implment a global error handler and it makes life easier. If you have .NET enabled on your web site, you can put a <customErrors /> element in the <system.web> configuration of the web.config file.
something like:
<customErrors defaultRedirect ="GenericError.htm">
<error statusCode ="404" redirect ="PageNotFound.htm" />
<error statusCode ="500" redirect ="AppError.htm" />
</customErrors>
This will at least ensure that people are not seeing the stacktrace or server out. The other place to set that is in the IIS / virtual directory application configuration.. you can point it to custom files and or scripts.
If you are going all classic ASP, the best way to do error handling in line is what someone suggested earlier up in this thread.. On Error Resume Next is the key..
There are some whacky aspects of it though, that make it not as useful as it should be.. the idea if you say On Error Resume Next in your script, it will not fail directly on an error, but will instead populate an ASP object called "Err" that has a few properties like .Number and .Description that allow you to do what you want with the error.
The only problem is, its hard to trap individual errors in a stack like you get with the whole try/catch bubble up effect. So generally what I would do is one of two things..
1. you set On Error Resume Next at the begining of the script.. and at the very end you check the Err.Number property for something greater than 0... if there is an error, than convert it to your friendly message.
2. if you need a more grainular approach, like you have a series of tasks that could pass or fail and you need to know them individually.. what you can do is call On Error Resume Next at the begining of each of your custom functions. This resets the ASP Err object and that way you can create your own effecient Pass/fail collection and it gives you a bit of isolation of errors in each function.
An example template that I would use for unit testing functions in Classic ASP looks a little like this:
Code:
On Error Resume Next
'TEST FUNCTION TEMPLATE'
Function Template(ByRef HTMLResults, ByRef ErrorMsg) 'As Boolean
If Err.Number <> 0 Then
Template = False
Exit Function
End If
On Error Resume Next 'Resets the Err object for isolated error trapping
HTMLResults = HTMLResults & "<tr><td><b>Doing Something..</b></td>"
'Do Some code or something here..
If Err.Number <> 0 Or ErrorMsg <> "" Then
Template = False
If Err.Number <> 0 Then
ErrorMsg = "Template(): " & Err.Number & " / " & Err.Description
End If
HTMLResults = HTMLResults & "<td><font color='red'><b>FAILED! [msg]:</b><font size='1'>" & ErrorMsg & "</font></font></td></tr>" & vbCrLF
Else
HTMLResults = HTMLResults & "<td><font color='green'><b>Passed</b></td></tr>" & vbCrLF
Template = True
End If
End Function 'Template
Obviously it has some of my own html for the test harness the script is from, but you should be able to get the idea..
Hope that helps!
Last edited by RabidSniper; 06-23-2008 at 03:30 PM..
|