refactor(通过功能): 异常提示优化
This commit is contained in:
parent
2b792c94d0
commit
109a8026d2
|
@ -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<ResultHolder> handlerMSException(Exception e) {
|
||||
public ResponseEntity<ResultHolder> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,6 +149,11 @@ public abstract class BaseTest {
|
|||
return JSON.parseObject(JSON.toJSONString(data), clazz);
|
||||
}
|
||||
|
||||
protected <T> T getResultMessageDetail(MvcResult mvcResult, Class<T> clazz) throws Exception {
|
||||
Object data = JSON.parseMap(mvcResult.getResponse().getContentAsString()).get("messageDetail");
|
||||
return JSON.parseObject(JSON.toJSONString(data), clazz);
|
||||
}
|
||||
|
||||
protected <T> List<T> getResultDataArray(MvcResult mvcResult, Class<T> 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: ====================================");
|
||||
}
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue