使用原因
我在生产上看到大量的try…catch语句
try {
/***
这里是屎山
这里是屎山
这里是屎山
这里是屎山
**/
} catch (Exception e) {
return ApiResult.fail();
}
解决方案自定义AOP+注解统一异常日志管理
Error注解
/**
* @Title: Error
* @Author sn
* @Package com.sn.hbase.ano
* @Date 2024/4/7 20:54
* @description:
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Error {
int code() default 0; //这个是用来自定义异常返回的信息
}
ErrorAspect.java 切面
/**
* @Title: Aspect
* @Author sn
* @Package com.sn.hbase.ano
* @Date 2024/4/7 20:54
* @description:
*/
@Aspect
@Component
public class ErrorAspect {
@Around("@annotation(error)")
public Object around(ProceedingJoinPoint joinPoint, Error error) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
String key = SpElUtils.getMethodKey(method);
Object proceed = null;
try {
proceed = joinPoint.proceed();
} catch (Exception e) {
return ApiResult.fail(error.code(),key+CommonErrorEnum.of(error.code()).getErrorMsg());
}
return proceed;
}
}
SpElUtils 工具类
/**
* @Title: SpElUtils
* @Author sn
* @Package com.sn.hbase.ano
* @Date 2024/4/7 20:54
* @description:
*/
public class SpElUtils {
public static String getMethodKey(Method method) {
return method.getDeclaringClass() + "#" + method.getName();
}
}
ErrorEnum接口
public interface ErrorEnum {
Integer getErrorCode();
String getErrorMsg();
}
CommonErrorEnum.java实现类
@AllArgsConstructor
@Getter
public enum CommonErrorEnum implements ErrorEnum {
BUSINESSION_ERROR(0, "{0}"),
PARAM_INVALID(-2, "参数校验失败"),
SYSTEM_ERROR(-1, "系统错误");
private final Integer code;
private final String msg;
@Override
public Integer getErrorCode() {
return code;
}
@Override
public String getErrorMsg() {
return msg;
}
private static Map<Integer, CommonErrorEnum> cache;
static {
cache = Arrays.stream(CommonErrorEnum.values()).collect(Collectors.toMap(CommonErrorEnum::getCode, Function.identity()));
}
public static CommonErrorEnum of(Integer type) {
return cache.get(type);
}
}
ApiResult.java
/**
* Description: 通用返回体
*/
@Data
@ApiModel("基础返回体")
public class ApiResult<T> {
@ApiModelProperty("成功标识true or false")
private Boolean success;
@ApiModelProperty("错误码")
private Integer errCode;
@ApiModelProperty("错误消息")
private String errMsg;
@ApiModelProperty("返回对象")
private T data;
public static <T> ApiResult<T> success() {
ApiResult<T> result = new ApiResult<T>();
result.setData(null);
result.setSuccess(Boolean.TRUE);
return result;
}
public static <T> ApiResult<T> success(T data) {
ApiResult<T> result = new ApiResult<T>();
result.setData(data);
result.setSuccess(Boolean.TRUE);
return result;
}
public static <T> ApiResult<T> fail(Integer code, String msg) {
ApiResult<T> result = new ApiResult<T>();
result.setSuccess(Boolean.FALSE);
result.setErrCode(code);
result.setErrMsg(msg);
return result;
}
public static <T> ApiResult<T> fail(ErrorEnum error) {
ApiResult<T> result = new ApiResult<T>();
result.setSuccess(Boolean.FALSE);
result.setErrCode(error.getErrorCode());
result.setErrMsg(error.getErrorMsg());
return result;
}
public boolean isSuccess() {
return this.success;
}
}
这里给大家举个例子,比如在Controller
@RestController
@RequestMapping("/aspect")
@Api(tags = "aspect相关接口")
public class HbaseController {
@GetMapping("/success")
@ApiOperation("ascpect")
@Error
public ApiResult<?> success() {
List<Integer> list = new ArrayList<>();
list.add(1);
return ApiResult.success(list);
}
@GetMapping("/error")
@ApiOperation("ascpect")
@Error
public ApiResult<?> error(){
List<Integer> list = new ArrayList<>();
list.add(1);
// int a = 1/0;
return ApiResult.success(list);
}
}
测试结果如下
觉得不错的可以看我下一篇进阶写法AOP+注解+函数式编程实现分布式锁