0

I am working on one project that requires me to execute some commands on remote server. I am using Laravel 5.5 with package name "laravelcollective/remote" that uses SSH2 to connect with the remote server.

However, I am facing some really weird issues with some servers. I get the following error message on some of the servers.

production.ERROR: Connection closed prematurely {"exception":"[object] (ErrorException(code: 0): Connection closed prematurely at /home/username/application_name/public_html/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php:3821, RuntimeException(code: 0): Unable to connect to remote server. at /home/username/application_name/public_html/vendor/laravelcollective/remote/src/Connection.php:143)

I am using try-catch block to catch exceptions but I am unable to catch this exception. All other Exceptions like connection timed out are being caught by the try-catch block except this one.

I am using try catch block like this:

try {
    $commands = array('sudo apt-get update','sudo apt-get upgrade -y');
    SSH::run($commands);
} catch (\Exception $e){
    report($e);
}

But the try-catch block stops working with this connection closed prematurely error. I don't know if I am missing something or there is some bug with the library, has anyone faced the same issue before? How can I catch this error the right way?

Adarsh Sojitra
  • 2,059
  • 1
  • 27
  • 50

1 Answers1

0

it's because an exception is not thrown, just an error. and an error cannot be caught.

I suggest you set a global error handler that converts all the errors to exceptions, as seen in this answer here and it would be a good idea to read other answers to that question too.

Generate a custom exception handler and then in a provider or in your public/index.php set your error handler

Fatemeh Majd
  • 719
  • 8
  • 22