feat:[REQ-3282] 增加异常处理类
This commit is contained in:
parent
597aa0319c
commit
275d3319c8
@ -18,7 +18,9 @@ public enum BizResultCode implements IResultCode {
|
||||
|
||||
// 900 ~ 998 二方SDK相关错误码
|
||||
RPC_ERROR("900", "rpc error"),
|
||||
DATA_PUMP_ERROR("999", "数据同步错误"),
|
||||
|
||||
|
||||
UNKNOWN_ERROR("999", "未知异常"),
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -0,0 +1,64 @@
|
||||
package cn.axzo.orgmanax.server;
|
||||
|
||||
import cn.axzo.foundation.exception.BusinessException;
|
||||
import cn.axzo.foundation.result.ApiResult;
|
||||
import cn.axzo.orgmanax.common.config.BizResultCode;
|
||||
import cn.azxo.framework.common.model.CommonResponse;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class ExceptionControllerAdvice {
|
||||
|
||||
private static final String ERROR_STACK_PATTERN = "(?s).*Exception.*";
|
||||
private static final String DEFAULT_MSG = "操作失败,请重试或联系安心筑工作人员。";
|
||||
|
||||
/**
|
||||
* 捕捉Exception异常
|
||||
*
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ApiResult<?> exceptionHandler(Exception ex) {
|
||||
log.error("未知异常", ex);
|
||||
return ApiResult.error(BizResultCode.UNKNOWN_ERROR.getErrorCode(), extractMessage(ex));
|
||||
}
|
||||
|
||||
/**
|
||||
* 捕捉Exception异常
|
||||
*
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public ApiResult<?> exceptionHandler(BusinessException ex) {
|
||||
log.error("业务异常", ex);
|
||||
return ApiResult.error(ex.getErrorCode(), extractMessage(ex));
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取错误信息
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
private String extractMessage(Exception e) {
|
||||
boolean isStackTrace = isStackTrace(e.getMessage());
|
||||
return isStackTrace ? DEFAULT_MSG : StrUtil.emptyToDefault(e.getMessage(), DEFAULT_MSG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为栈信息
|
||||
* @param errorMessage
|
||||
* @return
|
||||
*/
|
||||
private boolean isStackTrace(String errorMessage) {
|
||||
// 返回匹配结果
|
||||
return ReUtil.isMatch(ERROR_STACK_PATTERN, errorMessage);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user