Ad

How To Get A Proper Error Message When SFTP Host Is Invalid

- 1 answer

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.

public function testConnection(Request $request)
{
    $data = $request->all();
    $this->protocol = $data['protocol'];
    $this->host = $data['domain_name'];
    $this->username = $data['username'];
    $this->password = $data['password'];
    $this->port = $data['port'];
    $returnData = $this->checkFtp();
    return response()->json(['status' => true, 'connectionStatus' => $returnData]);
}

public function checkFtp()
{
    // Variable initialization
    $returnData = 'success';

    // SFTP connection test
    if ($this->protocol === 'sftp') {
        $sftp = new SFTP($this->host, $this->port, $this->timeout);
        if ($sftp) {
            if (!$sftp->login($this->username, $this->password)) {
                $returnData = 'Unable to login to the SFTP server';
            }
        } else {
            $returnData = 'Unable to connect to SFTP server';
        }
    }
}

When a user provides an invalid host, Internal server error is happening. So I can't get a message in returns to display.

How can I get a proper error message in return of this function?

Sample (While providing actual credentials)

{  
   "status":true,
   "connectionStatus":"success"
}

Internal server error (While giving a wrong hostname or IP)

{
"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": "->"
    },
    {
        "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": "->"
    },

Update: I have rewritten the function by adding try -- catch, but still i am getting the same error...

// SFTP connection test
    if ($this->protocol === 'sftp') {
        try {
            $sftp = new SFTP($this->host, $this->port, $this->timeout);
            if ($sftp) {
                if (!$sftp->login($this->username, $this->password)) {
                    $returnData = 'Unable to login to the SFTP server';
                }
            } else {
                $returnData = 'Unable to connect to SFTP server';
            }
        } catch(Exception $e) {
            $returnData = $e->getMessage();
        }
    }
Ad

Answer

I think you can handle this within subscribe() like the one below. The 500 internal error message will get in 'error'. just console that 'error' to view the message

this.service.functionname(this.form.value).subscribe(data => {
    console.log(data);
}, error => {
    console.log(error);
});
Ad
source: stackoverflow.com
Ad