diff --git a/axzo-common-autoconfigure/src/main/java/cn/axzo/framework/autoconfigure/web/exception/support/GlobalExceptionHandler.java b/axzo-common-autoconfigure/src/main/java/cn/axzo/framework/autoconfigure/web/exception/support/GlobalExceptionHandler.java index 55a035b..df3f6c9 100644 --- a/axzo-common-autoconfigure/src/main/java/cn/axzo/framework/autoconfigure/web/exception/support/GlobalExceptionHandler.java +++ b/axzo-common-autoconfigure/src/main/java/cn/axzo/framework/autoconfigure/web/exception/support/GlobalExceptionHandler.java @@ -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 err = err(512, "discovery server internal error"); + ApiReportResult err = err(512, "discovery server internal error"); Map recordExceptionMap = new HashMap<>(); recordExceptionMap.put(StringUtils.hasText(applicationName) ? applicationName : "not applicationName found", rebuildThrowable(e, request)); - err.getAnalysis().putAll(recordExceptionMap); + err.setAnalysis(recordExceptionMap); return err; } diff --git a/axzo-common-domain/src/main/java/cn/axzo/framework/domain/web/result/ApiCoreResult.java b/axzo-common-domain/src/main/java/cn/axzo/framework/domain/web/result/ApiCoreResult.java index 436fab7..55c90d4 100644 --- a/axzo-common-domain/src/main/java/cn/axzo/framework/domain/web/result/ApiCoreResult.java +++ b/axzo-common-domain/src/main/java/cn/axzo/framework/domain/web/result/ApiCoreResult.java @@ -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 implements Result { @ApiModelProperty(value = "业务码", required = true, position = 1) @@ -37,15 +37,6 @@ public abstract class ApiCoreResult implements Result { @ApiModelProperty(value = "业务数据", position = 100) protected E data; - @ApiModelProperty(value = "异常记录信息", position = 101) - protected Map 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 implements Result { map.put("code", code); map.put("msg", msg); map.put("data", data); - map.put("analysis", analysis); return map; } diff --git a/axzo-common-domain/src/main/java/cn/axzo/framework/domain/web/result/ApiReportResult.java b/axzo-common-domain/src/main/java/cn/axzo/framework/domain/web/result/ApiReportResult.java new file mode 100644 index 0000000..266ad7f --- /dev/null +++ b/axzo-common-domain/src/main/java/cn/axzo/framework/domain/web/result/ApiReportResult.java @@ -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 extends ApiCoreResult { + + @ApiModelProperty(value = "异常记录信息", position = 101) + protected Map analysis = new HashMap<>(); + + public static ApiReportResult ok() { + return ok(null); + } + + public static ApiReportResult ok(E data) { + return build(SUCCESS, data); + } + + public static ApiReportResult err(IRespCode code) { + return err(code, code.getMessage()); + } + + public static ApiReportResult err(IRespCode code, String message) { + return err(Integer.parseInt(code.getRespCode()), message); + } + + public static ApiReportResult err(IRespCode code, E data) { + return build(Integer.parseInt(code.getRespCode()), code.getMessage(), data); + } + + public static ApiReportResult err(String message) { + return err(Integer.parseInt(SERVER_ERROR.getRespCode()), message); + } + + public static ApiReportResult err(Integer code, String message) { + if (code == null) { + return err(message); + } + return build(code, message, null); + } + + public static ApiReportResult build(IRespCode code) { + return build(code, null); + } + + public static ApiReportResult build(IRespCode code, E data) { + return build(Integer.parseInt(code.getRespCode()), code.getMessage(), data); + } + + public static ApiReportResult build(Integer code, String message, E data) { + return new ApiReportResult<>(code, message, data); + } + + public static ApiReportResult 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 getAnalysis() { + return analysis; + } + + public void setAnalysis(Map analysis) { + this.analysis = analysis; + } + + @Override + public Map toMap() { + Map map = super.toMap(); + map.put("analysis", getAnalysis()); + return map; + } +}