Question

When im attempting to deploy things using laravel 4.1's SSH/Remote classes occasionally i get this exception

[2014-01-03 18:26:21] production.ERROR: exception 'ErrorException' with message 'Connection closed by server' in /home/{user}/{location}/deploy/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php:918

I was wondering if there is a way to detect if it has failed to connect in order to attempt the connection again and or multiple times?

Any ideas?

Était-ce utile?

La solution

Laravel uses phpseclib for its SSH library. On connection failure, phpseclib executes user_error('Connection closed by server'); (see Net/SSH2.php, line ~911). Laravel has a global error handler that picks this up, and logs it (as you saw in your question).

Unfortunately, phpseclib triggers errors, instead of throwing exceptions. If they were exceptions, you could add a new condition to Laravel's error handling:

App::error(function(Exception $exception){
    Log::error($exception);
    if($exception instanceof PHPSecLibException){
         // Let's handle this
    }
});

This definitely would be the "right way" to do it, but these aren't true exceptions (they're generic Laravel exceptions that are generated on your behalf when errors are triggered).

Luckily, Laravel translates errors into exceptions on your behalf. See src/Illuminate/Exception/Handler.php (lines ~129-135). So we could just add a conditional based on the info you do have:

App::error(function(Exception $exception){
    Log::error($exception);
    if(($exception->getMessage() == "Connection closed by server") &&
        ($exception->getFile() == "/home/{user}/{location}/deploy/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php")){
         // Let's handle this
    }
});

Take a look at the Exception methods available.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top