优化异常信息获取逻辑.

This commit is contained in:
lijiahangmax
2024-08-24 19:59:45 +08:00
parent 1fca5a1912
commit 924b46b41a
18 changed files with 106 additions and 68 deletions

View File

@@ -10,17 +10,17 @@ package com.orion.visor.module.asset.enums;
public enum HostExtraSshAuthTypeEnum {
/**
* 默认证方式
* 默认证方式
*/
DEFAULT,
/**
* 自定义密钥
* 自定义密钥
*/
CUSTOM_KEY,
/**
* 自定义身份
* 自定义身份
*/
CUSTOM_IDENTITY,
@@ -28,14 +28,14 @@ public enum HostExtraSshAuthTypeEnum {
public static HostExtraSshAuthTypeEnum of(String type) {
if (type == null) {
return null;
return DEFAULT;
}
for (HostExtraSshAuthTypeEnum value : values()) {
if (value.name().equals(type)) {
return value;
}
}
return null;
return DEFAULT;
}
}

View File

@@ -1,7 +1,7 @@
package com.orion.visor.module.asset.enums;
/**
* 主机证类型 - ssh
* 主机证类型 - ssh
*
* @author Jiahang Li
* @version 1.0.0
@@ -10,17 +10,17 @@ package com.orion.visor.module.asset.enums;
public enum HostSshAuthTypeEnum {
/**
* 密码
* 密码
*/
PASSWORD,
/**
* 密钥
* 密钥
*/
KEY,
/**
* 身份
* 身份
*/
IDENTITY,
@@ -28,14 +28,14 @@ public enum HostSshAuthTypeEnum {
public static HostSshAuthTypeEnum of(String type) {
if (type == null) {
return null;
return PASSWORD;
}
for (HostSshAuthTypeEnum value : values()) {
if (value.name().equals(type)) {
return value;
}
}
return null;
return PASSWORD;
}
}

View File

@@ -30,14 +30,14 @@ public enum HostSshOsTypeEnum {
public static HostSshOsTypeEnum of(String type) {
if (type == null) {
return null;
return LINUX;
}
for (HostSshOsTypeEnum value : values()) {
if (value.name().equals(type)) {
return value;
}
}
return null;
return LINUX;
}
}

View File

@@ -55,10 +55,6 @@ public class HostSshConfigStrategy extends AbstractGenericsDataStrategy<HostSshC
@Override
protected void preValid(HostSshConfigModel model) {
// 验证认证类型
Valid.valid(HostSshAuthTypeEnum::of, model.getAuthType());
// 验证系统版本
Valid.valid(HostSshOsTypeEnum::of, model.getOsType());
// 验证编码格式
this.validCharset(model.getCharset());
this.validCharset(model.getFileNameCharset());

View File

@@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON;
import com.orion.lang.exception.AuthenticationException;
import com.orion.lang.exception.ConnectionRuntimeException;
import com.orion.lang.exception.SftpException;
import com.orion.lang.exception.argument.InvalidArgumentException;
import com.orion.lang.support.timeout.TimeoutChecker;
import com.orion.lang.support.timeout.TimeoutEndpoint;
import com.orion.lang.utils.Booleans;
@@ -16,6 +15,7 @@ import com.orion.net.host.SessionStore;
import com.orion.net.host.sftp.SftpExecutor;
import com.orion.net.host.ssh.command.CommandExecutor;
import com.orion.spring.SpringHolder;
import com.orion.visor.framework.common.constant.ErrorMessage;
import com.orion.visor.framework.common.file.FileClient;
import com.orion.visor.module.asset.dao.ExecHostLogDAO;
import com.orion.visor.module.asset.entity.domain.ExecHostLogDO;
@@ -289,17 +289,25 @@ public abstract class BaseExecCommandHandler implements IExecCommandHandler {
* @return errorMessage
*/
protected String getErrorMessage(Exception ex) {
if (ex == null) {
return null;
}
String message;
if (ex instanceof InvalidArgumentException || ex instanceof IllegalArgumentException) {
if (ErrorMessage.isBizException(ex)) {
// 业务异常
message = ex.getMessage();
} else if (ex instanceof ConnectionRuntimeException) {
message = "连接失败";
// 连接异常
message = ErrorMessage.CONNECT_ERROR;
} else if (ex instanceof AuthenticationException) {
message = "认证失败";
// 认证异常
message = ErrorMessage.AUTH_ERROR;
} else if (ex instanceof SftpException) {
message = "脚本上传失败";
// 上传异常
message = ErrorMessage.SCRIPT_UPLOAD_ERROR;
} else {
message = "执行失败";
// 其他异常
message = ErrorMessage.EXEC_ERROR;
}
return Strings.retain(message, 250);
}

View File

@@ -53,7 +53,7 @@ public class SessionStores {
} catch (Exception e) {
String message = e.getMessage();
log.error("SessionStores-open-error hostId: {}, address: {}, username: {}, message: {}", hostId, address, username, message, e);
throw Exceptions.runtime(getErrorMessage(e), e);
throw Exceptions.app(getErrorMessage(e), e);
} finally {
CURRENT_ADDRESS.remove();
}
@@ -89,7 +89,10 @@ public class SessionStores {
SessionStore session = sessionHolder.getSession(conn.getHostAddress(), conn.getHostPort(), conn.getUsername());
// 使用密码认证
if (!useKey) {
session.password(CryptoUtils.decryptAsString(conn.getPassword()));
String password = conn.getPassword();
if (!Strings.isEmpty(password)) {
session.password(CryptoUtils.decryptAsString(password));
}
}
// 超时时间
session.timeout(conn.getTimeout());

View File

@@ -97,13 +97,8 @@ public abstract class AbstractTerminalHandler<T extends TerminalBasePayload> imp
* @return msg
*/
protected String getErrorMessage(Exception ex) {
if (ex == null) {
return null;
}
if (ex instanceof InvalidArgumentException || ex instanceof IllegalArgumentException) {
return ex.getMessage();
}
return ErrorMessage.OPERATE_ERROR;
// 获取错误信息
return ErrorMessage.getErrorMessage(ex, ErrorMessage.OPERATE_ERROR);
}
}

View File

@@ -1,7 +1,6 @@
package com.orion.visor.module.asset.handler.host.transfer.utils;
import com.alibaba.fastjson.JSON;
import com.orion.lang.exception.argument.InvalidArgumentException;
import com.orion.lang.utils.Strings;
import com.orion.visor.framework.common.constant.ErrorMessage;
import com.orion.visor.framework.websocket.core.utils.WebSockets;
@@ -62,13 +61,15 @@ public class TransferUtils {
public static String getErrorMessage(Exception ex) {
if (ex == null) {
return null;
} else if (ex instanceof InvalidArgumentException || ex instanceof IllegalArgumentException) {
} else if (ErrorMessage.isBizException(ex)) {
// 业务异常
String message = ex.getMessage();
if (Strings.isBlank(message)) {
return ErrorMessage.OPERATE_ERROR;
}
return message;
} else if (ex instanceof ClientAbortException) {
// 客户端主动断开
return ErrorMessage.CLIENT_ABORT;
}
return ErrorMessage.OPERATE_ERROR;

View File

@@ -3,7 +3,6 @@ package com.orion.visor.module.asset.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.exception.argument.InvalidArgumentException;
import com.orion.lang.id.UUIds;
import com.orion.lang.utils.Arrays1;
import com.orion.lang.utils.Objects1;
@@ -421,10 +420,8 @@ public class ExecLogServiceImpl implements ExecLogService {
} catch (Exception e) {
log.error("ExecLogService.downloadLogFile error id: {}", id, e);
Streams.close(in);
String errorMessage = ErrorMessage.FILE_READ_ERROR_CLEAR;
if (e instanceof InvalidArgumentException || e instanceof IllegalArgumentException) {
errorMessage = e.getMessage();
}
// 获取错误信息
String errorMessage = ErrorMessage.getErrorMessage(e, ErrorMessage.FILE_READ_ERROR_CLEAR);
// 响应错误信息
try {
Servlets.transfer(response, Strings.bytes(errorMessage), FileConst.ERROR_LOG);