title: How to get a proper error message when SFTP host is invalid
body: <p>I wrote a function in Laravel to test an SFTP connection with user-supplied server credentials. This function works fine and shows an appropriate message regarding connection status.</p>
  
  <pre><code>public function testConnection(Request $request)
  {
      $data = $request-&gt;all();
      $this-&gt;protocol = $data['protocol'];
      $this-&gt;host = $data['domain_name'];
      $this-&gt;username = $data['username'];
      $this-&gt;password = $data['password'];
      $this-&gt;port = $data['port'];
      $returnData = $this-&gt;checkFtp();
      return response()-&gt;json(['status' =&gt; true, 'connectionStatus' =&gt; $returnData]);
  }
  
  public function checkFtp()
  {
      // Variable initialization
      $returnData = 'success';
  
      // SFTP connection test
      if ($this-&gt;protocol === 'sftp') {
          $sftp = new SFTP($this-&gt;host, $this-&gt;port, $this-&gt;timeout);
          if ($sftp) {
              if (!$sftp-&gt;login($this-&gt;username, $this-&gt;password)) {
                  $returnData = 'Unable to login to the SFTP server';
              }
          } else {
              $returnData = 'Unable to connect to SFTP server';
          }
      }
  }
  </code></pre>
  
  <p>When a user provides an invalid host, Internal server error is happening. So I can't get a message in returns to display. </p>
  
  <p>How can I get a proper error message in return of this function?</p>
  
  <p><strong>Sample (While providing actual credentials)</strong></p>
  
  <pre><code>{  
     "status":true,
     "connectionStatus":"success"
  }
  </code></pre>
  
  <p><strong>Internal server error (While giving a wrong hostname or IP)</strong></p>
  
  <pre><code>{
  "message": "Cannot connect to invalid.host.com:22. Error 113. No route to host",
  "exception": "ErrorException",
  "file": "/var/www/html/rvhost_admin/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php",
  "line": 1139,
  "trace": [
      {
          "function": "handleError",
          "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
          "type": "-&gt;"
      },
      {
          "file": "/var/www/html/rvhost_admin/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php",
          "line": 1139,
          "function": "user_error"
      },
      {
          "file": "/var/www/html/rvhost_admin/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php",
          "line": 2158,
          "function": "_connect",
          "class": "phpseclib\\Net\\SSH2",
          "type": "-&gt;"
      },
  </code></pre>
  
  <p><strong>Update:</strong>
  I have rewritten the function by adding try -- catch, but still i am getting the same error...</p>
  
  <pre><code>// SFTP connection test
      if ($this-&gt;protocol === 'sftp') {
          try {
              $sftp = new SFTP($this-&gt;host, $this-&gt;port, $this-&gt;timeout);
              if ($sftp) {
                  if (!$sftp-&gt;login($this-&gt;username, $this-&gt;password)) {
                      $returnData = 'Unable to login to the SFTP server';
                  }
              } else {
                  $returnData = 'Unable to connect to SFTP server';
              }
          } catch(Exception $e) {
              $returnData = $e-&gt;getMessage();
          }
      }
  </code></pre>