From 109a8026d2db02b3dead7aad69cc1c0c64325615 Mon Sep 17 00:00:00 2001 From: jianxing Date: Fri, 14 Jul 2023 10:58:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=80=9A=E8=BF=87=E5=8A=9F=E8=83=BD):?= =?UTF-8?q?=20=E5=BC=82=E5=B8=B8=E6=8F=90=E7=A4=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RestControllerExceptionHandler.java | 27 +++++++++++++------ .../sdk/controller/handler/ResultHolder.java | 17 +++++------- .../sdk/src/test/java/base/BaseTest.java | 11 +++++--- .../system/controller/CommonParamTest.java | 4 +-- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/RestControllerExceptionHandler.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/RestControllerExceptionHandler.java index 074d235f9b..4f57231fdb 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/RestControllerExceptionHandler.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/RestControllerExceptionHandler.java @@ -16,6 +16,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.HashMap; import java.util.Map; @@ -68,26 +70,25 @@ public class RestControllerExceptionHandler { if (errorCode == null) { // 如果抛出异常没有设置状态码,则返回错误 message return ResponseEntity.internalServerError() - .body(new ResultHolder(MsHttpResultCode.FAILED.getCode(), - MsHttpResultCode.FAILED.getMessage(), e.getMessage())); + .body(ResultHolder.error(MsHttpResultCode.FAILED.getCode(), e.getMessage())); } if (errorCode instanceof MsHttpResultCode) { // 如果是 MsHttpResultCode,则设置响应的状态码,取状态码的后三位 return ResponseEntity.status(errorCode.getCode() % 1000) - .body(new ResultHolder(errorCode.getCode(), errorCode.getMessage(), e.getMessage())); + .body(ResultHolder.error(errorCode.getCode(), errorCode.getMessage())); } else { // 响应码返回 500,设置业务状态码 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body(new ResultHolder(errorCode.getCode(), errorCode.getMessage(), e.getMessage())); + .body(ResultHolder.error(errorCode.getCode(), errorCode.getMessage(), e.getMessage())); } } @ExceptionHandler({Exception.class}) - public ResponseEntity handlerMSException(Exception e) { + public ResponseEntity handlerException(Exception e) { return ResponseEntity.internalServerError() - .body(new ResultHolder(MsHttpResultCode.FAILED.getCode(), - MsHttpResultCode.FAILED.getMessage(), e.getMessage())); + .body(ResultHolder.error(MsHttpResultCode.FAILED.getCode(), + e.getMessage(), getStackTraceAsString(e))); } /*=========== Shiro 异常拦截==============*/ @@ -104,5 +105,15 @@ public class RestControllerExceptionHandler { return ResultHolder.error(HttpStatus.FORBIDDEN.value(), exception.getMessage()); } - + /** + * 格式化异常信息 + * 当出现未知异常时,将错误栈信息格式化返回 + * @param e + * @return + */ + public static String getStackTraceAsString(Exception e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + return sw.toString(); + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/ResultHolder.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/ResultHolder.java index 949c6db8f9..6f16f0ee75 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/ResultHolder.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/handler/ResultHolder.java @@ -1,25 +1,24 @@ package io.metersphere.sdk.controller.handler; +import io.metersphere.sdk.controller.handler.result.MsHttpResultCode; import lombok.Data; @Data public class ResultHolder { // 请求是否成功 - private int code = 0; + private int code = MsHttpResultCode.SUCCESS.getCode(); // 描述信息,一般是错误信息 private String message; // 详细描述信息, 如有异常,这里是详细日志 - private String messageDetail; + private Object messageDetail; // 返回数据 private Object data = ""; public ResultHolder() { - this.code = 0; } public ResultHolder(Object data) { this.data = data; - this.code = 0; } public ResultHolder(int code, String msg) { @@ -33,7 +32,7 @@ public class ResultHolder { this.data = data; } - public ResultHolder(int code, String msg, String messageDetail, Object data) { + public ResultHolder(int code, String msg, Object messageDetail, Object data) { this.code = code; this.message = msg; this.messageDetail = messageDetail; @@ -45,18 +44,14 @@ public class ResultHolder { } public static ResultHolder error(int code, String message) { - return new ResultHolder(code, message, null); - } - - public static ResultHolder error(int code, String message, Object data) { - return new ResultHolder(code, message, data); + return new ResultHolder(code, message, null, null); } public static ResultHolder error(String message, String messageDetail) { return new ResultHolder(-1, message, messageDetail, null); } - public static ResultHolder error(int code, String message, String messageDetail) { + public static ResultHolder error(int code, String message, Object messageDetail) { return new ResultHolder(code, message, messageDetail, null); } } diff --git a/backend/framework/sdk/src/test/java/base/BaseTest.java b/backend/framework/sdk/src/test/java/base/BaseTest.java index 5e07b94232..a73371447d 100644 --- a/backend/framework/sdk/src/test/java/base/BaseTest.java +++ b/backend/framework/sdk/src/test/java/base/BaseTest.java @@ -149,6 +149,11 @@ public abstract class BaseTest { return JSON.parseObject(JSON.toJSONString(data), clazz); } + protected T getResultMessageDetail(MvcResult mvcResult, Class clazz) throws Exception { + Object data = JSON.parseMap(mvcResult.getResponse().getContentAsString()).get("messageDetail"); + return JSON.parseObject(JSON.toJSONString(data), clazz); + } + protected List getResultDataArray(MvcResult mvcResult, Class clazz) throws Exception { Object data = JSON.parseMap(mvcResult.getResponse().getContentAsString()).get("data"); return JSON.parseArray(JSON.toJSONString(data), clazz); @@ -229,10 +234,10 @@ public abstract class BaseTest { MvcResult mvcResult = this.requestPostAndReturn(path, invalidateParamInfo.getValue()); // 校验错误是否是参数错误 Assertions.assertEquals(400, mvcResult.getResponse().getStatus()); - Map resultData = getResultData(mvcResult, Map.class); - System.out.println("result: " + resultData); + Map messageDetail = getResultMessageDetail(mvcResult, Map.class); + System.out.println("result: " + messageDetail); // 校验错误信息中包含了该字段 - Assertions.assertTrue(resultData.containsKey(invalidateParamInfo.getName())); + Assertions.assertTrue(messageDetail.containsKey(invalidateParamInfo.getName())); } System.out.println("paramValidateTest-end: ===================================="); } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CommonParamTest.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CommonParamTest.java index 26fd87f7c0..ec7f5f3ae3 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CommonParamTest.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CommonParamTest.java @@ -35,8 +35,8 @@ class CommonParamTest extends BaseTest { MvcResult mvcResult = this.requestPostAndReturn(url, basePageRequestDefinition); // 校验错误是否是参数错误 Assertions.assertEquals(400, mvcResult.getResponse().getStatus()); - Map resultData = getResultData(mvcResult, Map.class); + Map messageDetail = getResultMessageDetail(mvcResult, Map.class); // 校验错误信息中包含了该字段 - Assertions.assertTrue(resultData.containsKey(String.format("sort[%s]", sortName))); + Assertions.assertTrue(messageDetail.containsKey(String.format("sort[%s]", sortName))); } } \ No newline at end of file