Q:

Handle 'ssh2_auth_password()' error upon wrong credentials

I'm kind of struggling to when it comes to handle error on ssh2_auth_password() function (and ssh2_connect() as well). I manage successfully on connecting and login to my remote server, however, when it comes to supply wrong credentials, I would like to be able to handle those cases. I'm using ZF 1.12 and I've created a little controller plugin as so: 
<?php
class My_Controller_Plugin_Scp extends Zend_Controller_Plugin_Abstract
{
    private $host;
    private $login;
    private $password;
    private $destDir = '.';
    private $connexion;

    public function __construct()
    {
        $co = ssh2_connect($this->getHost(), 22);
        if(false === $co)
            throw new Exception('Can\'t connect to remote server');

        $this->setConnexion($co);
    }

    /**
     * @return bool
     * @throws Exception
     */
    public function auth()
    {
        $auth = ssh2_auth_password($this->getConnexion(), $this->getLogin(), $this->getPassword());

        if($auth === false)
            throw new Exception('Authentication failed!');

        return $auth;
    }

And I'm using this plugin in my controller like this:
    try {
        // Préparation à la copie en SSH du fichier template
        $scp = new My_Controller_Plugin_Scp();
        $scp->auth(); // Authentification
    } catch(Exception $e) {
        echo $e->getMessage();
        $this->getHelper('Redirector')->setGotoRoute(array(), 'ficheVoir');
        exit();
    }

The actual output is, when I mess up the credentials, a PHP warning is thrown whereas I would like to be able to display a custom error message.
Is there something I'm doing wrong with Exception and stuff?

A:

I was not able to test your code because a lot of things were missing.
I wrote a simple connect function based on your code:
<?php

function connect() {
    $host = '';
    $login = '';
    $password = '';
    $co = ssh2_connect($host, 22);
    if (false === $co)
        throw new Exception('Can\'t connect to remote server');
    $result = @ssh2_auth_password($co, $login, $password);
    var_dump($result);
    if ($result === false) {
        throw new Exception('Authentication failed!');
    }
    echo 'Connection estabilished';
}

try {
    connect();
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
}

I am suppressing PHP warnings using @ not to throw ugly PHP error messages. If $result is false an Exception is thrown and is being handled properly. Basically, the solution is just to add @ before ssh2_auth_password() function.

