优化异常信息获取逻辑.

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

@@ -1,5 +1,8 @@
package com.orion.visor.framework.common.constant;
import com.orion.lang.exception.ApplicationException;
import com.orion.lang.exception.argument.InvalidArgumentException;
/**
* 错误信息
*
@@ -97,6 +100,14 @@ public interface ErrorMessage {
String TASK_ABSENT = "任务不存在";
String CONNECT_ERROR = "连接失败";
String AUTH_ERROR = "认证失败";
String SCRIPT_UPLOAD_ERROR = "脚本上传失败";
String EXEC_ERROR = "执行失败";
String ILLEGAL_STATUS = "当前状态不支持此操作";
String CHECK_AUTHORIZED_HOST = "请选择已授权的主机";
@@ -111,4 +122,41 @@ public interface ErrorMessage {
String UNABLE_DOWNLOAD_FOLDER = "无法下载文件夹";
/**
* 是否为业务异常
*
* @param ex ex
* @return biz exception
*/
static boolean isBizException(Exception ex) {
if (ex == null) {
return false;
}
return ex instanceof InvalidArgumentException
|| ex instanceof IllegalArgumentException
|| ex instanceof ApplicationException;
}
/**
* 获取错误信息
*
* @param ex ex
* @param defaultMsg defaultMsg
* @return message
*/
static String getErrorMessage(Exception ex, String defaultMsg) {
if (ex == null) {
return null;
}
String message = ex.getMessage();
if (message == null) {
return defaultMsg;
}
// 业务异常
if (isBizException(ex)) {
return message;
}
return defaultMsg;
}
}

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);

View File

@@ -28,21 +28,11 @@ interface XHRCustom extends XHR {
Mock.XHR.prototype.send = (() => {
// @ts-ignore
const _send = Mock.XHR.prototype.send;
const defaultEvent = () => {
};
return function (this: XHRCustom) {
if (!this.match) {
this.custom.xhr.responseType = this.responseType || '';
this.custom.xhr.timeout = this.timeout || 0;
this.custom.xhr.withCredentials = this.withCredentials || false;
this.custom.xhr.onabort = this.onabort || defaultEvent;
this.custom.xhr.onerror = this.onerror || defaultEvent;
this.custom.xhr.onload = this.onload || defaultEvent;
this.custom.xhr.onloadend = this.onloadend || defaultEvent;
this.custom.xhr.onloadstart = this.onloadstart || defaultEvent;
this.custom.xhr.onprogress = this.onprogress || defaultEvent;
this.custom.xhr.onreadystatechange = this.onreadystatechange || defaultEvent;
this.custom.xhr.ontimeout = this.ontimeout || defaultEvent;
}
return _send.apply(this, arguments);
};

View File

@@ -43,9 +43,9 @@
:disabled="SshAuthType.IDENTITY === formModel.authType"
placeholder="请输入用户名" />
</a-form-item>
<!-- 证方式 -->
<!-- 证方式 -->
<a-form-item field="authType"
label="证方式"
label="证方式"
:hide-asterisk="true">
<a-radio-group type="button"
class="auth-type-group usn"
@@ -165,7 +165,7 @@
formModel.value.useNewPassword = !formModel.value.hasPassword;
});
// 用户名
// 用户名
const usernameRules = [{
validator: (value, cb) => {
if (value && value.length > 128) {
@@ -179,7 +179,7 @@
}
}] as FieldRule[];
// 密码
// 密码
const passwordRules = [{
validator: (value, cb) => {
if (value && value.length > 256) {

View File

@@ -14,17 +14,17 @@ export interface HostSshConfig {
hasPassword?: boolean;
}
// 主机证方式
// 主机证方式
export const SshAuthType = {
// 密码
// 密码
PASSWORD: 'PASSWORD',
// 密钥
// 密钥
KEY: 'KEY',
// 身份
// 身份
IDENTITY: 'IDENTITY'
};
// 主机证方式 字典项
// 主机证方式 字典项
export const sshAuthTypeKey = 'hostSshAuthType';
// 主机系统类型 字典项

View File

@@ -127,7 +127,7 @@
defineExpose({ openAdd, openUpdate });
// 密码
// 密码
const passwordRules = [{
validator: (value, cb) => {
if (value && value.length > 512) {

View File

@@ -163,7 +163,7 @@
defineExpose({ openAdd, openUpdate, openView });
// 密码
// 密码
const passwordRules = [{
validator: (value, cb) => {
if (value && value.length > 512) {

View File

@@ -4,8 +4,8 @@
label-align="right"
:label-col-props="{ span: 5 }"
:wrapper-col-props="{ span: 18 }">
<!-- 证方式 -->
<a-form-item field="authType" label="证方式">
<!-- 证方式 -->
<a-form-item field="authType" label="证方式">
<a-radio-group type="button"
v-model="formModel.authType"
:options="toRadioOptions(extraSshAuthTypeKey)" />

View File

@@ -36,7 +36,7 @@ export default class TerminalOutputProcessor implements ITerminalOutputProcessor
});
} else {
// 未成功展示错误信息
ssh.write(`${msg || ''}\r\n输入回车重新连接...\r\n\r\n`);
ssh.write(`${msg || ''}\r\n\r\n输入回车重新连接...\r\n\r\n`);
ssh.status = TerminalStatus.CLOSED;
}
}, sftp => {
@@ -69,7 +69,7 @@ export default class TerminalOutputProcessor implements ITerminalOutputProcessor
ssh.connect();
} else {
// 未成功展示错误信息
ssh.write(`${msg || ''}\r\n输入回车重新连接...\r\n\r\n`);
ssh.write(`${msg || ''}\r\n\r\n输入回车重新连接...\r\n\r\n`);
ssh.status = TerminalStatus.CLOSED;
}
}, sftp => {

View File

@@ -18,7 +18,7 @@ INSERT INTO `dict_key` VALUES (7, 'systemMenuCache', 'INTEGER', '[]', '菜单缓
INSERT INTO `dict_key` VALUES (8, 'dictValueType', 'STRING', '[{\"name\": \"color\", \"type\": \"COLOR\"}]', '字典配置值类型', '2023-10-27 01:48:51', '2023-10-27 01:48:51', '1', '1', 0);
INSERT INTO `dict_key` VALUES (9, 'systemUserStatus', 'INTEGER', '[{\"name\": \"color\", \"type\": \"COLOR\"}]', '用户状态', '2023-10-27 12:10:41', '2023-10-27 12:10:41', '1', '1', 0);
INSERT INTO `dict_key` VALUES (10, 'systemRoleStatus', 'INTEGER', '[{\"name\": \"color\", \"type\": \"COLOR\"}, {\"name\": \"status\", \"type\": \"STRING\"}]', '角色状态', '2023-10-27 12:33:04', '2023-10-27 12:33:17', '1', '1', 0);
INSERT INTO `dict_key` VALUES (11, 'hostSshAuthType', 'STRING', '[]', '主机ssh身份证方式', '2023-10-27 14:29:12', '2023-12-25 15:41:40', '1', '1', 0);
INSERT INTO `dict_key` VALUES (11, 'hostSshAuthType', 'STRING', '[]', '主机ssh身份证方式', '2023-10-27 14:29:12', '2023-12-25 15:41:40', '1', '1', 0);
INSERT INTO `dict_key` VALUES (15, 'operatorLogResult', 'INTEGER', '[{\"name\": \"color\", \"type\": \"COLOR\"}]', '操作日志结果', '2023-10-31 17:35:28', '2023-10-31 17:42:50', '2', '2', 0);
INSERT INTO `dict_key` VALUES (16, 'operatorRiskLevel', 'STRING', '[{\"name\": \"color\", \"type\": \"COLOR\"}]', '操作风险等级', '2023-11-01 16:03:00', '2023-11-01 16:03:00', '1', '1', 0);
INSERT INTO `dict_key` VALUES (19, 'systemMenuNewWindow', 'INTEGER', '[]', '菜单是否开启新窗口', '2023-12-05 14:14:29', '2023-12-05 14:14:29', '1', '1', 0);
@@ -68,9 +68,9 @@ INSERT INTO `dict_value` VALUES (17, 9, 'systemUserStatus', '0', '停用', '{\"c
INSERT INTO `dict_value` VALUES (18, 9, 'systemUserStatus', '1', '启用', '{\"color\": \"arcoblue\"}', 20, '2023-10-27 12:13:17', '2024-04-24 16:35:00', '1', '1', 0);
INSERT INTO `dict_value` VALUES (20, 10, 'systemRoleStatus', '0', '停用', '{\"color\": \"orange\", \"status\": \"danger\"}', 10, '2023-10-27 12:33:45', '2023-10-27 12:33:45', '1', '1', 0);
INSERT INTO `dict_value` VALUES (21, 10, 'systemRoleStatus', '1', '启用', '{\"color\": \"arcoblue\", \"status\": \"default\"}', 20, '2023-10-27 12:33:56', '2024-04-24 16:34:52', '1', '1', 0);
INSERT INTO `dict_value` VALUES (22, 11, 'hostSshAuthType', 'PASSWORD', '密码', '{}', 10, '2023-10-27 14:29:28', '2023-12-25 15:40:47', '1', '1', 0);
INSERT INTO `dict_value` VALUES (23, 11, 'hostSshAuthType', 'KEY', '密钥', '{}', 20, '2023-10-27 14:29:35', '2024-05-17 12:48:53', '1', '1', 0);
INSERT INTO `dict_value` VALUES (24, 11, 'hostSshAuthType', 'IDENTITY', '身份', '{}', 30, '2023-10-27 14:29:42', '2023-12-25 15:40:47', '1', '1', 0);
INSERT INTO `dict_value` VALUES (22, 11, 'hostSshAuthType', 'PASSWORD', '密码', '{}', 10, '2023-10-27 14:29:28', '2023-12-25 15:40:47', '1', '1', 0);
INSERT INTO `dict_value` VALUES (23, 11, 'hostSshAuthType', 'KEY', '密钥', '{}', 20, '2023-10-27 14:29:35', '2024-05-17 12:48:53', '1', '1', 0);
INSERT INTO `dict_value` VALUES (24, 11, 'hostSshAuthType', 'IDENTITY', '身份', '{}', 30, '2023-10-27 14:29:42', '2023-12-25 15:40:47', '1', '1', 0);
INSERT INTO `dict_value` VALUES (55, 1, 'operatorLogModule', 'infra:authentication', '身份认证', '{}', 1000, '2023-10-31 10:47:48', '2023-10-31 10:55:12', '1', '1', 0);
INSERT INTO `dict_value` VALUES (56, 1, 'operatorLogModule', 'infra:system-user', '系统用户', '{}', 1010, '2023-10-31 10:47:51', '2023-10-31 11:00:59', '1', '1', 0);
INSERT INTO `dict_value` VALUES (57, 1, 'operatorLogModule', 'infra:system-role', '系统角色', '{}', 1020, '2023-10-31 10:47:52', '2023-10-31 10:54:59', '1', '1', 0);