@Override @ServiceExceptionHandler public OperateResult createProcessTask(TaskInfoDO taskInfoDO) { Boolean isTaskDistribution = UserHolder.getUser().getIsTaskDistribution(); if (null==isTaskDistribution||!isTaskDistribution){ throw new ErrorCodeException(DailyManageErrorCode.NO_AUTH_ERROR.withArgs("创建任务")); } }
@Aspect @Component public class ServiceExceptionHandleAspect { @Around(value = "@annotation(serviceExceptionHandler)", argNames = "serviceExceptionHandler") public Object around(ProceedingJoinPoint pjp, ServiceExceptionHandler serviceExceptionHandler) throws Throwable { Signature signature = pjp.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method targetMethod = methodSignature.getMethod(); String className = targetMethod.getDeclaringClass().getName(); String methodName = targetMethod.getName(); ErrorLogInfoBuilder errorLogInfoBuilder = ErrorLogInfoBuilder.instance().ClassName(className).MethodName(methodName); //获取方法的参数 Object[] args = pjp.getArgs(); if(null != args && args.length > 0){ for(int i=0; i
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ServiceExceptionHandler { String name() default ""; }
public class ErrorCodeException extends RuntimeException{ /** * */ private static final long serialVersionUID = 1L; private ErrorCode errorCode; public ErrorCodeException(ErrorCode errorCode, Throwable throwable) { super(errorCode.getMessage(), throwable); this.errorCode = errorCode; } public ErrorCodeException(ErrorCode errorCode) { super(errorCode.getMessage(), null); this.errorCode = errorCode; } public ErrorCodeException(String code, String message, Throwable throwable) { super(message, throwable); this.errorCode = new DefaultErrorCode(code, message); } public ErrorCode getErrorCode() { return errorCode; } }
public enum DailyManageErrorCode implements ErrorCode { /** * 请求参数异常 */ PARAMETER_ERROR("DailyManageErrorCode.REQUEST_PARAMETER_ERROR","请求参数异常:%s"), /** * 没有访问权限 */ NO_AUTH_ERROR("InstitutionalOverviewErrorCode.NO_AUTH_ERROR","用户没有%s权限"), /** * 请求参数跟数据库重复 */ PARAMETER_DUPLICATION("DailyManageErrorCode.PARAMETER_DUPLICATION","%s填写重复") ; private String errorCode; private String errorMessage; private String[] args; DailyManageErrorCode(String errorCode, String errorMessage) { this.errorCode = errorCode; this.errorMessage = errorMessage; } public DailyManageErrorCode withArgs(String... args) { this.args = args; return this; } @Override public String getCode() { return errorCode; } @Override public String getMessage() { return String.format(errorMessage, (Object[])args); } }