From 3051af68e0dee346bba7741fe2b641d12419c271 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Thu, 18 Apr 2024 15:37:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E8=B5=84=E6=BA=90=E6=B1=A0=E6=89=A7=E8=A1=8C=E5=89=8D?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E5=BD=93=E5=89=8D=E7=AB=99=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1039322 --user=陈建星 【接口测试】系统-资源池-错误的当前站点url-使用该资源池执行接口,一直在加载中状态 https://www.tapd.cn/55049933/s/1501013 --- .../sdk/exception/TaskRunnerResultCode.java | 28 +++++++++++++++++++ .../main/resources/i18n/commons.properties | 3 +- .../resources/i18n/commons_en_US.properties | 3 +- .../resources/i18n/commons_zh_CN.properties | 3 +- .../resources/i18n/commons_zh_TW.properties | 3 +- .../api/service/ApiExecuteService.java | 27 ++++++++++++++---- 6 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/exception/TaskRunnerResultCode.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/exception/TaskRunnerResultCode.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/exception/TaskRunnerResultCode.java new file mode 100644 index 0000000000..645753f0df --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/exception/TaskRunnerResultCode.java @@ -0,0 +1,28 @@ +package io.metersphere.sdk.exception; + +/** + * @author jianxing + */ +public enum TaskRunnerResultCode implements IResultCode { + + MS_URL_NOT_AVAILABLE(200001, "ms_url_not_available"); + + + private final int code; + private final String message; + + TaskRunnerResultCode(int code, String message) { + this.code = code; + this.message = message; + } + + @Override + public int getCode() { + return code; + } + + @Override + public String getMessage() { + return getTranslationMessage(this.message); + } +} diff --git a/backend/framework/sdk/src/main/resources/i18n/commons.properties b/backend/framework/sdk/src/main/resources/i18n/commons.properties index bcbc2d42f0..30d2ff1d61 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons.properties @@ -527,4 +527,5 @@ relate_source_type_not_blank=关联资源类型不能为空 api_import_schedule=接口定义-定时导入任务 project.description.length_range=项目描述长度必须在{min}和{max}之间 -api_test_environment_datasource_connect_failed=数据源连接失败 \ No newline at end of file +api_test_environment_datasource_connect_failed=数据源连接失败 +ms_url_not_available=资源池无法访问当前站点 \ No newline at end of file diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties index 804f486f06..9406e02b93 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties @@ -565,4 +565,5 @@ project.description.length_range=The description must be between {min} and {max} api_test_environment_datasource_connect_failed=Data source connection failed permission.api_definition.delete_and_recover=Delete/Recover -permission.service_integration.reset=Reset \ No newline at end of file +permission.service_integration.reset=Reset +ms_url_not_available=The resource pool cannot access the current site diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties index 8dbc2b3e55..d51940ccd9 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties @@ -562,4 +562,5 @@ project.description.length_range=项目描述长度必须在{min}和{max}之间 api_test_environment_datasource_connect_failed=数据源连接失败 permission.api_definition.delete_and_recover=删除/恢复 -permission.service_integration.reset=重置 \ No newline at end of file +permission.service_integration.reset=重置 +ms_url_not_available=资源池无法访问当前站点 \ No newline at end of file diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties index 0a7cb404a5..02d9f23254 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties @@ -562,4 +562,5 @@ api_import_schedule=接口導入定時任務 project.description.length_range=項目描述長度必須在{min}和{max}之間 permission.api_definition.delete_and_recover=刪除/恢復 -permission.service_integration.reset=重置 \ No newline at end of file +permission.service_integration.reset=重置 +ms_url_not_available=資源池無法訪問當前站點 \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index 089e5fd2f2..3eaa49f8bc 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -27,11 +27,13 @@ import io.metersphere.sdk.dto.api.task.ApiExecuteFileInfo; import io.metersphere.sdk.dto.api.task.ApiRunModeConfigDTO; import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.exception.TaskRunnerResultCode; import io.metersphere.sdk.file.FileCenter; import io.metersphere.sdk.file.FileRepository; import io.metersphere.sdk.file.FileRequest; import io.metersphere.sdk.util.*; import io.metersphere.system.config.MinioProperties; +import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.domain.TestResourcePool; import io.metersphere.system.dto.pool.TestResourceNodeDTO; import io.metersphere.system.dto.pool.TestResourcePoolReturnDTO; @@ -50,6 +52,7 @@ import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.client.HttpServerErrorException; import java.io.File; import java.io.IOException; @@ -164,19 +167,31 @@ public class ApiExecuteService { try { return doExecute(taskRequest); + } catch (HttpServerErrorException e) { + handleDoExecuteException(scriptRedisKey, e); + int errorCode = e.getResponseBodyAs(ResultHolder.class).getCode(); + for (TaskRunnerResultCode taskRunnerResultCode : TaskRunnerResultCode.values()) { + // 匹配资源池的错误代码,抛出相应异常 + if (taskRunnerResultCode.getCode() == errorCode) { + throw new MSException(taskRunnerResultCode, e.getMessage()); + } + } + throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage()); } catch (MSException e) { - LogUtils.error(e); - // 调用失败清理脚本 - stringRedisTemplate.delete(scriptRedisKey); + handleDoExecuteException(scriptRedisKey, e); throw e; } catch (Exception e) { - LogUtils.error(e); - // 调用失败清理脚本 - stringRedisTemplate.delete(scriptRedisKey); + handleDoExecuteException(scriptRedisKey, e); throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage()); } } + private void handleDoExecuteException(String scriptRedisKey, Exception e) { + LogUtils.error(e); + // 调用失败清理脚本 + stringRedisTemplate.delete(scriptRedisKey); + } + private GlobalParams getGlobalParam(String projectId) { GlobalParamsDTO globalParamsDTO = globalParamsService.get(projectId); if (globalParamsDTO != null) {