Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error trapping 1

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Hi

In our logon script I have this line of code:
Code:
intReturn = objWSHShell.Run("zwscript.exe " & varNetLogonPath & "z" & strPlugIn, 0, FALSE)
I want to check for errors generally throughout the script and so have written this code:
Code:
if err <> 0 then
    strCodeLine = "intReturn = objWSHShell.Run(""wscript.exe "" & varNetLogonPath & strPlugIn, 0, FALSE)" &_
                  vbCrLf &_
                  "as" & vbCrLf &_
                  "intReturn = objWSHShell.Run(wscript.exe " & varNetLogonPath & strPlugIn & ", 0, FALSE)"
    strFriendlyError = "Unable to launch PlugIn '" & varNetLogonPath & strPlugIn & "'"
    strErrResponse = HandleNonFatalError(Err.Number,Err.Source,Err.Description,strCodeLine,strFriendlyError)
    err.clear
else
    WriteLog("  Called PlugIn '" & strPlugIn & "' with error number '" & err & "' (zero means no error)")
end if
The function 'HandleNonFatalError' is as follows:
Code:
Function HandleNonFatalError(varErrNumber,strErrSource,strErrDescription,strCodeLine,strFriendlyError)
    strErrResponse = DisplayErrorInfo(varErrNumber,strErrSource,strErrDescription,strCodeLine)
    WriteLog(vbCrLf)
    'QuitOut
End Function
(There is also 'HandleFatalError' which doesn't have the 'QuitOut' function call commented out).

My question is this, why doesn't the [tt]intReturn = objWSHShell.Run("zwscript.exe " & varNetLogonPath & "z" & strPlugIn, 0, FALSE)[/tt] line return any kind of error code/value even though it quite plainly has a typo in it? I've also tried typos in each of the two elements in the target path but it just never seem to produce an actual error. At 'best' it just ignores the whole routine that the objWSHShell.run code is in (even though it's got a list of files to iterate through).

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Replace this:
intReturn = objWSHShell.Run("zwscript.exe " & varNetLogonPath & "z" & strPlugIn, 0, FALSE)
with this:
intReturn = objWSHShell.Run("zwscript.exe " & varNetLogonPath & "z" & strPlugIn, 0, True)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hmm, well having looked up the .run method I can see why you've suggested that, but, two things.

1) I'm still not getting an error code returned even with typos to create an 'erroneous' condition
2) I really don't want the main script to be waiting around for the called script to finish before moving on.

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
I think we need to see the code for DisplayErrorInfo, as I'm guessing that you are using On Error Resume Next and I think you are encountering an additional error in the error 'handling' functions you are calling.
 
Code:
Function DisplayErrorInfo(varErrNumber,strErrSource,strErrDescription,strCodeLine)
    Dim strResponse, ErrMsg
    WScript.Echo "  An error occurred...!" ' when processing line:" & vbCrLf & strCodeLine
    ErrMsg = "An error occurred when parsing line:" & vbCrLf & strCodeLine &_
             vbCrLf &_
             vbCrLf & "Error" & vbTab & vbTab & varErrNumber &_
             vbCrLf & "Error (hex)" & vbTab &  "&H" & Hex(varErrNumber) &_
             vbCrLf & "Source" & vbTab & vbTab & strErrSource &_
             vbCrLf & "Description" & vbTab & strErrDescription
    'strResponse = MsgBox(ErrMsg,vbCritical,strAppDescription & " - I'm sorry, but there was an error")
    strResponse = MsgBox(strFriendlyError & vbCrLf & vbCrLf & "Please contact IT Support",vbCritical,strAppDescription & " - I'm sorry, but there was an error")
    ErrMsg = vbcrLf & ErrMsg
    WriteLog(ErrMsg)
    Err.Clear
End Function

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
If the conclusion is drawn based on the strErrResponse return from HandleNonFatalError() of any kind, the whole return value mechanism is severely broken down. The return value mechanism is wrong.

If HandleNonFatalError() is meant to return a message to the variable strErrResponse, it is this.
[tt]
Function HandleNonFatalError(varErrNumber,strErrSource,strErrDescription,strCodeLine,strFriendlyError)
[red]dim strErrResponsep 'restrict its scope[/red]
[red]'but better still is to not to design names colliding everywhere[/red]
strErrResponse = DisplayErrorInfo(varErrNumber,strErrSource,strErrDescription,strCodeLine)
WriteLog(vbCrLf)
'QuitOut
[red]HandleNonFatalError=strErrResponse 'this is the true return value[/red]

End Function
[/tt]
Same for DisplayErrorInfo(), where does it have return value?
 
They don't have return values so they could just as well be Subs.

This code does work at other parts in the script, such as when creating an object, so I'm wondering if the focus on the sub/functions is the wrong area. If it works in other places in the logon script, but doesn't it work for the .Run method then isn't the issue with the .Run method itself?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
The .Run method always return 0 if the 3rd argument is False.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
There is not a doubt that if in the run method, the executable is not found, for instance, it will be reported in the err object. The intReturn is meant to capture any managed error reporting which must be specifically designed "into" the command line. In that case, the synchoroneity is necessary, as PHV noted, 3rd parameter must be put to true, otherwise, intReturn always return 0, no matter what.
 
I would further say modulation is good, fragmentation is not.
 
Right, taking on board tsuji's last comment I've combined three subs/functions into one:
Code:
Set objDomain = getObject("LDAP://rootDse")
if err <> 0 then
    strCodeLine = "Set objDomain = getObject(""LDAP://rootDse"")"
    strFriendlyError = "Unable to create Domain Object. Sorry, but this is a serious error, so quitting!"
    strErrResponse = HandleError(Err.Number,Err.Source,Err.Description,strCodeLine,strFriendlyError,TRUE)
end if

Set objPlugIns = objFSO.OpenTextFile(strPlugIns, ForReading)
if err <> 0 then
    strCodeLine = "Set objPlugIns = objFSO.OpenTextFile(strPlugIns, ForReading)" &_
                  vbCrLf & "as" & vbCrLf & "Set objPlugIns = objFSO.OpenTextFile(" & strPlugIns & ", " & ForReading & ")"
    strFriendlyError = "Unable to create PlugIns Object. Not a serious error, but can't run Plug-ins without this file." & vbCrLf & vbCrLf &_
                       "The logon script will continue but please let IT know."
    strErrResponse = HandleError(Err.Number,Err.Source,Err.Description,strCodeLine,strFriendlyError,FALSE)
end if
'-----------------------------------------------------------
Sub HandleError(varErrNumber,strErrSource,strErrDescription,strCodeLine,strFriendlyError,bQuit)
    'strErrResponse = DisplayErrorInfo(varErrNumber,strErrSource,strErrDescription,strCodeLine)
    'DisplayErrorInfo(varErrNumber,strErrSource,strErrDescription,strCodeLine)
    Dim strResponse, ErrMsg
    WScript.Echo "  An error occurred...!" ' when processing line:" & vbCrLf & strCodeLine
    ErrMsg = "An error occurred when parsing line:" & vbCrLf & strCodeLine &_
             vbCrLf &_
             vbCrLf & "Error" & vbTab & vbTab & varErrNumber &_
             vbCrLf & "Error (hex)" & vbTab &  "&H" & Hex(varErrNumber) &_
             vbCrLf & "Source" & vbTab & vbTab & strErrSource &_
             vbCrLf & "Description" & vbTab & strErrDescription
    'strResponse = MsgBox(ErrMsg,vbCritical,strAppDescription & " - I'm sorry, but there was an error")
    strResponse = MsgBox(strFriendlyError & vbCrLf & vbCrLf & "Please contact IT Support",vbCritical,strAppDescription & " - I'm sorry, but there was an error")
    ErrMsg = vbcrLf & ErrMsg
    WriteLog(ErrMsg)
    Err.Clear
    WriteLog(vbCrLf)
    if bQuit = TRUE then
        QuitOut
    end if
End Sub
But now I find that HandleError isn't actually doing anything when I create false error conditions. And of course this still leaves me with the issue of how to handle error trapping for called external scripts when I don't want the main script to wait around for them...

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
>strErrResponse = HandleError(Err.Number,Err.Source,Err.Description,strCodeLine,strFriendlyError,TRUE)
It is type mismatched runtime. It should be written like this for a sub.
[tt]
HandleError Err.Number,Err.Source,Err.Description,strCodeLine,strFriendlyError,TRUE
'or
'call HandleError(Err.Number,Err.Source,Err.Description,strCodeLine,strFriendlyError,TRUE)[/tt]
 
Nice one, tsuji. Thank you. No more shall I have subs disguised as functions.

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top