异常处理扩展封装
This commit is contained in:
parent
81676a308c
commit
a2ae88a64f
@ -1,10 +1,12 @@
|
||||
package cn.axzo.framework.autoconfigure.web.exception.handler;
|
||||
|
||||
import cn.axzo.framework.autoconfigure.web.exception.RespErrorCodeMappingProperties;
|
||||
import cn.axzo.framework.domain.web.BizException;
|
||||
import cn.axzo.framework.domain.web.code.BaseCode;
|
||||
import cn.axzo.framework.domain.web.code.IRespCode;
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@ -15,6 +17,8 @@ import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static cn.axzo.framework.core.Constants.API_MARKER;
|
||||
import static cn.axzo.framework.core.enums.ErrorLevel.P0;
|
||||
import static cn.axzo.framework.core.enums.ErrorType.ERROR_SYSTEM;
|
||||
import static cn.axzo.framework.domain.web.code.BaseCode.SERVER_ERROR;
|
||||
import static cn.axzo.framework.domain.web.result.ApiResult.err;
|
||||
import static jodd.util.StringPool.ASTERISK;
|
||||
@ -28,6 +32,9 @@ import static org.springframework.http.HttpHeaders.CACHE_CONTROL;
|
||||
@Slf4j
|
||||
public abstract class AbstractExceptionApiResultHandler<T extends Throwable> implements ExceptionApiResultHandler<T>, Ordered {
|
||||
|
||||
private static final String TYPE_KEY = "errorType";
|
||||
private static final String LEVEL_KEY = "errorLevel";
|
||||
|
||||
// 基础错误响应码 -> HTTP状态码
|
||||
private final Map<String, HttpStatus> baseMapping;
|
||||
|
||||
@ -71,7 +78,7 @@ public abstract class AbstractExceptionApiResultHandler<T extends Throwable> imp
|
||||
log(status, request, errorMsg, error);
|
||||
|
||||
// 6.返回body
|
||||
return err(Integer.parseInt(code), message);
|
||||
return errBody(code, message);
|
||||
}
|
||||
|
||||
protected T getRealCause(T ex) {
|
||||
@ -108,7 +115,7 @@ public abstract class AbstractExceptionApiResultHandler<T extends Throwable> imp
|
||||
if (baseMapping.containsKey(ASTERISK)) {
|
||||
return baseMapping.get(ASTERISK);
|
||||
}
|
||||
if(baseMapping.containsKey(code)) {
|
||||
if (baseMapping.containsKey(code)) {
|
||||
return baseMapping.get(code);
|
||||
}
|
||||
return fallbackStatus;
|
||||
@ -124,13 +131,33 @@ public abstract class AbstractExceptionApiResultHandler<T extends Throwable> imp
|
||||
}
|
||||
|
||||
protected void log(HttpStatus status, HttpServletRequest request, String errorMsg, Throwable error) {
|
||||
if (status.is5xxServerError()) {
|
||||
log.error(API_MARKER, "Exception occurred: " + errorMsg + ". [URL=" + request.getRequestURI() + "]", error);
|
||||
if (error instanceof BizException) {
|
||||
String errorLevel = ((BizException) error).getErrorLevel();
|
||||
String errorType = ((BizException) error).getErrorType();
|
||||
logError(request, errorMsg, error, errorLevel, errorType);
|
||||
} else {
|
||||
log.warn(API_MARKER, errorMsg + ". [URL=" + request.getRequestURI() + "]");
|
||||
if (status.is5xxServerError()) {
|
||||
logError(request, errorMsg, error, P0.getValue(), ERROR_SYSTEM.getValue());
|
||||
} else {
|
||||
log.warn(API_MARKER, errorMsg + ". [URL=" + request.getRequestURI() + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void logError(HttpServletRequest request, String errorMsg, Throwable error, String errorLevel, String errorType) {
|
||||
try {
|
||||
MDC.put(TYPE_KEY, errorType);
|
||||
MDC.put(LEVEL_KEY, errorLevel);
|
||||
log.error(API_MARKER, "Exception occurred: " + errorMsg + ". [URL=" + request.getRequestURI() + "]", error);
|
||||
} finally {
|
||||
MDC.remove(TYPE_KEY);
|
||||
MDC.remove(LEVEL_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
protected ApiResult<?> errBody(String code, String message) {
|
||||
return err(Integer.parseInt(code), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
package cn.axzo.framework.core.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ErrorLevel {
|
||||
|
||||
P0("P0"),
|
||||
P1("P1"),
|
||||
P2("P2"),
|
||||
P3("P3"),
|
||||
P4("P4");
|
||||
|
||||
private final String value;
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package cn.axzo.framework.core.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ErrorType {
|
||||
|
||||
ERROR_SYSTEM("系统异常"),
|
||||
ERROR_BUSINESS("业务异常"),
|
||||
ERROR_INNER_SERVICE("服务间调用异常"),
|
||||
ERROR_THIRD_SERVICE("第三方服务异常"),
|
||||
ERROR_SQL("Sql异常"),
|
||||
ERROR_ES("Elasticsearch异常"),
|
||||
ERROR_REDIS("Redis异常"),
|
||||
ERROR_KAFKA("Kafka异常"),
|
||||
ERROR_RABBITMQ("RabbitMq异常"),
|
||||
ERROR_UNKNOWN("未知异常");
|
||||
|
||||
private final String value;
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package cn.axzo.framework.domain.web;
|
||||
|
||||
import cn.axzo.framework.core.enums.ErrorLevel;
|
||||
import cn.axzo.framework.core.enums.ErrorType;
|
||||
import cn.axzo.framework.domain.web.code.IRespCode;
|
||||
import lombok.Getter;
|
||||
|
||||
import static cn.axzo.framework.core.enums.ErrorLevel.P2;
|
||||
import static cn.axzo.framework.core.enums.ErrorType.ERROR_UNKNOWN;
|
||||
import static java.lang.String.format;
|
||||
|
||||
/**
|
||||
* @Author: liyong.tian
|
||||
* @Date: 2023/4/4 11:46
|
||||
* @Description: 业务类异常
|
||||
*/
|
||||
public class BizException extends RuntimeException {
|
||||
|
||||
@Getter
|
||||
private String code;
|
||||
|
||||
@Getter
|
||||
private String errorLevel;
|
||||
|
||||
@Getter
|
||||
private String errorType;
|
||||
|
||||
public BizException(IRespCode code, Object... args) {
|
||||
this(P2, ERROR_UNKNOWN, code, args);
|
||||
}
|
||||
|
||||
public BizException(ErrorLevel errorLevel, ErrorType errorType, IRespCode code, Object... args) {
|
||||
this(errorLevel, errorType, code, null, args);
|
||||
}
|
||||
|
||||
public BizException(ErrorLevel errorLevel, ErrorType errorType, IRespCode code, String message, Object... args) {
|
||||
super(format(message == null ? code.getMessage() : message, args));
|
||||
this.code = code.getRespCode();
|
||||
this.errorLevel = errorLevel.getValue();
|
||||
this.errorType = errorType.getValue();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user