Throwing errors under Perl

Advertisement

golfkilo
Joined:
Posts:
2

Throwing errors under Perl

Hi,

I have a problem with error handling in perl
All the latest versions of WinSCP, ActivePerl, Perl modules...

I am using this example with sftp server running under Centos. The Windows machine is 2012R2
https://winscp.net/eng/docs/library_perl#example

Well all works apart from error handling...

For error handling testing I tried non existing file names, etc

$transferResult->Check();

Nothing happens here... Nada. What am I missing?

I did the same tests with the powershell example script.. And it works exactly as expected!
( https://winscp.net/eng/docs/library_powershell#example )
It throws the errors like a dream.

The problem is I have to use perl... any idea what could be wrong? I suspect something in Win32::OLE;

Anyone got an idea?

Reply with quote

Advertisement

golfkilo
Joined:
Posts:
2

I think I have solved it myself :)

The sample code needs some amending...

At the beginning it needs to add for example:
Win32::OLE->Option(Warn => sub {goto CheckError});

And then have something like this at the end of your script...
CheckError:
#Just an example what you might want to do...
my $oleerror = Win32::OLE->LastError;
print "$oleerror\n";
$session->Dispose();
exit;

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
27,614
Location:
Prague, Czechia

Re: Throwing errors under Perl

Thanks for sharing this.
_________________
Martin Prikryl

Reply with quote

Ram
Guest

Hi golfkilo,

Tried plugging in the solution you posted for error handling. The perl script throws an error on Check Error. i.e. Can't find label CheckError

Would you be able to share some sample codes to catch the session open or transfer result errors please?

Win32::OLE->Option(Warn => sub {goto CheckError});

And then have something like this at the end of your script...
CheckError:
#Just an example what you might want to do...
my $oleerror = Win32::OLE->LastError;
print "$oleerror\n";
$session->Dispose();
exit;[/quote]

Reply with quote

RamAjay
Guest

I figured it out the error. I was calling Win32::OLE->Option(Warn => sub {goto CheckError}); from the sub routine which was calling the session->Open. I had to move that out of the sub routine to capture the errors.

i.e.
Win32::OLE->Option(Warn => sub {goto CheckError});
sub sftp{
Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);
my $session = Win32::OLE->new('WinSCP.Session') || die "Can't create winscp sessoion: ", Win32::OLE->LastError;
my $consts = Win32::OLE::Const->Load($session);
my $sessionOptions = Win32::OLE->new('WinSCP.SessionOptions')|| die "Can't open winscp
sessoionOptions: ", Win32::OLE->LastError;
$sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'};
$sessionOptions->{'HostName'} = $Server;
$sessionOptions->{'UserName'} = $UserId;
$sessionOptions->{'Password'} = $SSHPassword;
$sessionOptions->{'SshHostKeyFingerprint'} = $SSHHostKeyFingerPrint;

$session->Open( $sessionOptions);
my $OLEConnectionError = Win32::OLE->LastError;
if ("$OLEConnectionError" ne "0")
{
print "$OLEConnectionError","\n";
exit 13;
}

}

Reply with quote

Advertisement

You can post new topics in this forum