Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch un-handled errors with PHPseclib?

Tags:

php

phpseclib

Let's say I have the following piece of code.

To test this, I change the server IP to mimic the error messages. The IP below doesn't exist so the Unhandled Exception message is: Cannot connect to 10.199.1.7. Error 113. No route to host

This displays an ugly screen with PHP code. Is it possible to catch this error?

try {
      $ssh = new Net_SSH2('10.199.1.7');        
  if (!$ssh->login('deploy', $key)) {
       throw new Exception("Failed login");
  }
} catch (Exception $e) {
     ???
}
like image 579
sdot257 Avatar asked Nov 27 '12 16:11

sdot257


1 Answers

Looked through library.

user_error('Connection closed by server', E_USER_NOTICE);

It triggers errors. You can handle those errors using http://php.net/manual/en/function.set-error-handler.php

e.g.

// Your file.php
$ssh = new Net_SSH2('10.199.1.7');        
$ssh->login('deploy', $key);

// bootstrap.php
// This will catch all user notice errors!!!
set_error_handler ('errorHandler', E_USER_NOTICE)

function errorHandler($errno, $errstr, $errfile, $errline) {
    echo 'Error';
    // Whatever you want to do.
}
like image 89
E_p Avatar answered Sep 20 '22 17:09

E_p