http://www.perlmonks.org?node_id=295431
mikedshelton has asked for the wisdom of the Perl Monks concerning the following question:

Have read all the posts on Net::SFTP however still need assistance to catch errors when using $sftp->put()

$sftp->put() does not appear to return a status (success or failure) that can be caught in $@; I have confirmed client side info (file, directory) and connection (host, user, password) is correct

What I'd like to do is catch errors, check for errors caught, kill program when errors exist and write these errors to a log file and the screen.

use strict; use Net::SFTP; $sftp->put( $file, $target_file, \&callback ) or die( "\nDied : Error $!");

returns "Died..." everytime... BTW the file is written successfully to host and $! is null so tried wrapping with eval{} without success

eval { $sftp->put( $file, $target_file, \&callback ); }<br> if ( $@ ) { writelog(...) die("Died Here: Error $!"); }

When trying to write to a known invalid directory on host, we never make it in "if ($@)"

The errors (below) don't appear to be caught in $@.   Errors received are: Any assistance would be appreciated
Mike

janitored by ybiC: Fix broken <code> tags, unordered list and formatting of errors for legibility

Replies are listed 'Best First'.
Re: Net::SFTP does not catch errors
by idsfa (Vicar) on Sep 30, 2003 at 23:12 UTC

    Yup, put() sure doesn't return a status. The pod nods in the direction of saying so, by not indicating any return value. The code confirms this, with no return values for the put function. The lower level commands such as do_write, however, do return a status. You may need to roll your own ...

    $sfh = $sftp->do_open($target_file ...); . . . #loop over source file, reading blocks of $data $status = $sftp->do_write($sfh, $offset, $data) &bail_out($status) if ($status != SSH2_FX_OK); . . . $sftp->do_close($sfh);

    In fact, this is pretty much exactly what the put() method does, but it discards the $status. Might have a patch to submit there ...


    Remember, when you stare long into the abyss, you could have been home eating ice cream.
      bug reports submitted
Re: Net::SFTP does not catch errors
by lsherwood (Sexton) on Dec 29, 2007 at 22:19 UTC
    I've noticed a similar "feature" when using 'get' with Net::SFTP. Allow me to expand on your comment a bit, as I suspect 'get' and 'put' operate identically in this regard. Here is some code very similar to yours, using 'get':
    eval { $sftp->get($remote_file, $local_file) or die "GET FAILED"; }; print "eval says: $@" if $@;
    The problem is that with the 'die' statement present in the get command, the 'die' phrase is passed to $@ whether or not the get command succeeds. As best I can determine, this is a flaw, or at least a limitation, in Net::SFTP. In addition, even when not using the eval statement to enclose the sftp->get command, a die statement is executed even when the 'get' command succeeds. This kills the script right after completing the 'get'.

    Removing the 'die' statement but keeping the eval in place results in:

    1) No $@ generated if the get succeeds -- this is fine;

    2) $@ is created if the get fails and the problem is with the local file path;

    3) No $@ generated if the get fails and the problem is in the remote file path -- the get failure remains undetected by eval and so it is not stored in $@.

    It appears to me that:

    a) die should not be used with $sftp->get commands because even on a successful 'get';

    .... i) if eval is used, an unwanted $@ is created from the 'die' statement;

    .... ii) if eval is not used the 'die' kills the feed script (although the get succeeds);

    b) Eval should be used to generate $@ so that get 'failures' related to the local file path are detected;

    c) Using eval does not fully verify the get succeeded because it will ignore get failures due to problems with the remote file path.

    If anyone knows a workaround to this limitation with Net::SFTP, please post it here.