SFTP 主机无效时如何获取正确的错误消息
How to get a proper error message when SFTP host is invalid
我在 Laravel 中编写了一个函数来测试使用用户提供的服务器凭据的 SFTP 连接。此功能运行良好,并显示有关连接状态的适当消息。
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';
}
}
}
当用户提供无效主机时,发生内部服务器错误。所以我无法在 return 秒中显示消息。
如何在此函数的 return 中获得正确的错误消息?
示例(同时提供实际凭据)
{
"status":true,
"connectionStatus":"success"
}
内部服务器错误(同时提供错误的主机名或 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": "->"
},
更新:
我通过添加 try -- catch 重写了函数,但我仍然遇到同样的错误...
// 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();
}
}
我认为您可以在 subscribe() 中处理此问题,如下所示。 500 内部错误消息将出现在 'error' 中。只是安慰 'error' 查看消息
this.service.functionname(this.form.value).subscribe(data => {
console.log(data);
}, error => {
console.log(error);
});
我在 Laravel 中编写了一个函数来测试使用用户提供的服务器凭据的 SFTP 连接。此功能运行良好,并显示有关连接状态的适当消息。
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';
}
}
}
当用户提供无效主机时,发生内部服务器错误。所以我无法在 return 秒中显示消息。
如何在此函数的 return 中获得正确的错误消息?
示例(同时提供实际凭据)
{
"status":true,
"connectionStatus":"success"
}
内部服务器错误(同时提供错误的主机名或 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": "->"
},
更新: 我通过添加 try -- catch 重写了函数,但我仍然遇到同样的错误...
// 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();
}
}
我认为您可以在 subscribe() 中处理此问题,如下所示。 500 内部错误消息将出现在 'error' 中。只是安慰 'error' 查看消息
this.service.functionname(this.form.value).subscribe(data => {
console.log(data);
}, error => {
console.log(error);
});