fix(微服务平台): 用户输入环境变量格式异常时返回友好的中文提示信息

This commit is contained in:
OTTO 2025-09-10 09:53:39 +08:00
parent 1c9bc630d8
commit f402ec1748
2 changed files with 26 additions and 10 deletions

View File

@ -157,7 +157,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
case STOP: {
applicationSpecJson.put("replicas", 0);
try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) {
return RancherResponseHandler.handleBoolean(response, "启动应用");
return RancherResponseHandler.handleBoolean(response, "停止应用");
}
}
case FORCE_STOP: {
@ -178,7 +178,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
}
return delPodSuccess;
}
return RancherResponseHandler.handleBoolean(response, "启动应用");
return RancherResponseHandler.handleBoolean(response, "强制停止应用");
}
}
case RESTART: {
@ -211,7 +211,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
applicationDetailJson.put("spec", applicationSpecJson);
applicationDetailJson.put("metadata", metadataJson);
try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) {
return RancherResponseHandler.handleBoolean(response, "应用");
return RancherResponseHandler.handleBoolean(response, "启应用");
}
}
@ -221,14 +221,14 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
applicationSpecJson.put("paused", true);
applicationDetailJson.put("spec", applicationSpecJson);
try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) {
return RancherResponseHandler.handleBoolean(response, "启动应用");
return RancherResponseHandler.handleBoolean(response, "暂停应用");
}
}
case RESUME: {
applicationSpecJson.put("paused", false);
applicationDetailJson.put("spec", applicationSpecJson);
try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) {
return RancherResponseHandler.handleBoolean(response, "启动应用");
return RancherResponseHandler.handleBoolean(response, "恢复应用");
}
}
}
@ -252,7 +252,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
public Boolean createApplication(String kind, String yamlContent) {
ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind);
try (Response response = remoteRancherService.createK8sApplication(applicationType.getFullName(), yamlContent, SecurityContextHolder.getUserKey())) {
return RancherResponseHandler.handleBoolean(response, "启动应用");
return RancherResponseHandler.handleBoolean(response, "创建应用");
}
}
@ -274,7 +274,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
public Boolean deleteApplication(String kind, String namespace, String applicationName) {
ApplicationType applicationType = ApplicationType.getApplicationTypeByKey(kind);
try (Response response = remoteRancherService.deleteApplication(applicationType.getFullName(), namespace, applicationName, SecurityContextHolder.getUserKey())) {
return RancherResponseHandler.handleBoolean(response, "启动应用");
return RancherResponseHandler.handleBoolean(response, "删除应用");
}
}
@ -416,7 +416,7 @@ public class MicroManagementServiceImpl extends CommonService implements IMicroM
try (Response response = remoteRancherService.updateK8sApplication(applicationType.getFullName(), namespace, applicationName, applicationDetailJson, SecurityContextHolder.getUserKey())) {
return RancherResponseHandler.handleBoolean(response, "启动应用");
return RancherResponseHandler.handleBoolean(response, "更新应用环境变量","环境变量格式异常");
}
}

View File

@ -2,6 +2,7 @@ package com.microservices.mon.k8s.utils;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.system.api.utils.FeignUtils;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
@ -13,7 +14,7 @@ import java.nio.charset.StandardCharsets;
@Slf4j
public class RancherResponseHandler {
public static JSONObject handleJsonObject(Response response, String functionName) {
public static JSONObject handleJsonObject(Response response, String functionName, String errorMessage) {
if (response != null) {
Response.Body body = response.body();
if (body != null) {
@ -25,7 +26,12 @@ public class RancherResponseHandler {
}
if (bodyJson != null && bodyJson.containsKey("message")) {
log.error("{}时发生错误:{}", functionName, bodyJson.toJSONString());
throw new ServiceException("%s时发生错误错误原因%s", functionName, bodyJson.getString("message"));
if (StringUtils.isEmpty(errorMessage)) {
throw new ServiceException("%s时发生错误错误原因%s", functionName, bodyJson.getString("message"));
} else {
throw new ServiceException("%s时发生错误错误原因%s", functionName, errorMessage);
}
}
} catch (IOException e) {
throw new ServiceException("%s时发生错误响应数据读取异常", functionName);
@ -36,10 +42,20 @@ public class RancherResponseHandler {
throw new ServiceException("%s时发生错误请联系系统管理员", functionName);
}
public static JSONObject handleJsonObject(Response response, String functionName) {
return handleJsonObject(response, functionName, null);
}
public static Boolean handleBoolean(Response response, String functionName) {
JSONObject bodyJson = handleJsonObject(response, functionName);
return bodyJson != null;
}
public static Boolean handleBoolean(Response response, String functionName, String errorMessage) {
JSONObject bodyJson = handleJsonObject(response, functionName, errorMessage);
return bodyJson != null;
}
}