How to catch errors correctly? server unreachable, fingerprint wrong, access denied, etc.

Advertisement

cakruege
Donor
Joined:
Posts:
2

How to catch errors correctly? server unreachable, fingerprint wrong, access denied, etc.

Hi,

I'm using this powershell code example:
library_powershell#module

I'm trying to differentiate between errors.
For example:
-host unreachable
-fingerprint wrong
-access denied

I replaced Exception with WinSCP.SessionRemoteException
catch [WinSCP.SessionRemoteException]
{
   Write-Host "WinSCP.SessionRemoteException"
    Write-Host $_.Exception.Message
    exit 1
}
but every of these 3 failures is a WinSCP.SessionRemoteException. -> didn't help

add failed handler:
function Failed
{
   param($e)
   
   $e
}

$session.add_Failed( { Failed($_) } )

Failed method didn't get called for every of these 3 failures -> didn't help

    finally
    {
        # Disconnect, clean up
        $session.output
        $session.Dispose()
    }
Session.output has the correct information but it's bad parseable text.
Sometimes it is the last line, sometimes not.

What do I do wrong?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: How to catch errors correctly? server unreachable, fingerprint wrong, access denied, etc.

The Session.Failed is called all these scenarios.

Indeed the mere $e expression does not yield any output. I do not know why, I do not know PowerShell good enough. But Write-Host $e does.
Anyway, it's the same information as you get from the exception, so it won't help.

The best you can do is to parse Exception message (not log).

Reply with quote

cakruege
Donor
Joined:
Posts:
2

Re: How to catch errors correctly? server unreachable, fingerprint wrong, access denied, etc.

martin wrote:


Indeed the mere $e expression does not yield any output.
Thx. You're right.

martin wrote:


Anyway, it's the same information as you get from the exception, so it won't help.
Hmm, that's not what I wanted to hear :cry:

I'd like to suggest a new feature:
For example new exceptions types that inherit from SessionRemoteException
HostNotFoundException
WrongFingerprint
AccessDenied

or new Failure2-Event with proper states

martin wrote:


The best you can do is to parse Exception message (not log).

That would be my temporary solution

Reply with quote

martin
Site Admin
martin avatar

Re: How to catch errors correctly? server unreachable, fingerprint wrong, access denied, etc.

OK, I'll consider that.

Reply with quote

Advertisement

You can post new topics in this forum