feat(REQ-2516): 异常信息通过新的字段进行响应
This commit is contained in:
parent
d3b36b08bf
commit
c5af495c89
@ -6,7 +6,7 @@ import cn.axzo.framework.core.InternalException;
|
||||
import cn.axzo.framework.core.RecordException;
|
||||
import cn.axzo.framework.core.util.ClassUtil;
|
||||
import cn.axzo.framework.domain.web.code.BaseCode;
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import cn.axzo.framework.domain.web.result.ApiReportResult;
|
||||
import cn.axzo.framework.domain.web.result.Result;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
@ -54,7 +54,7 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.axzo.framework.domain.web.result.ApiResult.err;
|
||||
import static cn.axzo.framework.domain.web.result.ApiReportResult.err;
|
||||
import static cn.axzo.framework.web.filter.BasicRecordExceptionFilter.MICRO_SERVER_RECORD_ERROR_FILTER_PACKAGE_VALUE;
|
||||
import static cn.axzo.framework.web.filter.BasicRecordExceptionFilter.MICRO_SERVER_RECORD_ERROR_GET_PARAM_NAME;
|
||||
import static java.lang.String.format;
|
||||
@ -297,10 +297,10 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
private Result buildResult(HttpServletRequest request, HttpServletResponse response, Throwable e) {
|
||||
response.setHeader(CACHE_CONTROL, "no-store");
|
||||
response.setStatus(512);
|
||||
ApiResult<Object> err = err(512, "discovery server internal error");
|
||||
ApiReportResult<Object> err = err(512, "discovery server internal error");
|
||||
Map<String, ExceptionWrapper> recordExceptionMap = new HashMap<>();
|
||||
recordExceptionMap.put(StringUtils.hasText(applicationName) ? applicationName : "not applicationName found", rebuildThrowable(e, request));
|
||||
err.getAnalysis().putAll(recordExceptionMap);
|
||||
err.setAnalysis(recordExceptionMap);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ import static jodd.util.StringUtil.isNotBlank;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonPropertyOrder({"code", "msg", "data", "analysis"})
|
||||
@JsonPropertyOrder({"code", "msg", "data"})
|
||||
public abstract class ApiCoreResult<E> implements Result {
|
||||
|
||||
@ApiModelProperty(value = "业务码", required = true, position = 1)
|
||||
@ -37,15 +37,6 @@ public abstract class ApiCoreResult<E> implements Result {
|
||||
@ApiModelProperty(value = "业务数据", position = 100)
|
||||
protected E data;
|
||||
|
||||
@ApiModelProperty(value = "异常记录信息", position = 101)
|
||||
protected Map<String, Object> analysis = new HashMap<>();
|
||||
|
||||
protected ApiCoreResult(Integer code, String msg, E data) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Transient
|
||||
protected IRespCode[] getOKCodes() {
|
||||
return new IRespCode[]{BaseCode.SUCCESS};
|
||||
@ -87,7 +78,6 @@ public abstract class ApiCoreResult<E> implements Result {
|
||||
map.put("code", code);
|
||||
map.put("msg", msg);
|
||||
map.put("data", data);
|
||||
map.put("analysis", analysis);
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,100 @@
|
||||
package cn.axzo.framework.domain.web.result;
|
||||
|
||||
import cn.axzo.framework.domain.web.ApiException;
|
||||
import cn.axzo.framework.domain.web.code.IRespCode;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.beans.ConstructorProperties;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static cn.axzo.framework.domain.web.code.BaseCode.SERVER_ERROR;
|
||||
import static cn.axzo.framework.domain.web.code.BaseCode.SUCCESS;
|
||||
|
||||
/**
|
||||
* @Description
|
||||
* @Author liyong.tian
|
||||
* @Date 2020/9/7 20:33
|
||||
**/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class ApiReportResult<E> extends ApiCoreResult<E> {
|
||||
|
||||
@ApiModelProperty(value = "异常记录信息", position = 101)
|
||||
protected Map<String, ?> analysis = new HashMap<>();
|
||||
|
||||
public static <E> ApiReportResult<E> ok() {
|
||||
return ok(null);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> ok(E data) {
|
||||
return build(SUCCESS, data);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> err(IRespCode code) {
|
||||
return err(code, code.getMessage());
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> err(IRespCode code, String message) {
|
||||
return err(Integer.parseInt(code.getRespCode()), message);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> err(IRespCode code, E data) {
|
||||
return build(Integer.parseInt(code.getRespCode()), code.getMessage(), data);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> err(String message) {
|
||||
return err(Integer.parseInt(SERVER_ERROR.getRespCode()), message);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> err(Integer code, String message) {
|
||||
if (code == null) {
|
||||
return err(message);
|
||||
}
|
||||
return build(code, message, null);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> build(IRespCode code) {
|
||||
return build(code, null);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> build(IRespCode code, E data) {
|
||||
return build(Integer.parseInt(code.getRespCode()), code.getMessage(), data);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> build(Integer code, String message, E data) {
|
||||
return new ApiReportResult<>(code, message, data);
|
||||
}
|
||||
|
||||
public static <E> ApiReportResult<E> with(Throwable e) {
|
||||
if (e != null && e instanceof ApiException) {
|
||||
return err(((ApiException) e).getCode(), e.getMessage());
|
||||
}
|
||||
if (e != null) {
|
||||
return err(SERVER_ERROR, e.getMessage());
|
||||
}
|
||||
return err(SERVER_ERROR);
|
||||
}
|
||||
|
||||
public ApiReportResult(Integer code, String msg, E data) {
|
||||
super(code, msg, data);
|
||||
}
|
||||
|
||||
public Map<String, ?> getAnalysis() {
|
||||
return analysis;
|
||||
}
|
||||
|
||||
public void setAnalysis(Map<String, ?> analysis) {
|
||||
this.analysis = analysis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> map = super.toMap();
|
||||
map.put("analysis", getAnalysis());
|
||||
return map;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user