码迷,mamicode.com
首页 > 其他好文 > 详细

自定义异常 状态码 以及Aop拦截Apect

时间:2019-06-05 20:07:02      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:poi   return   format   hold   访问   inter   document   aspect   val   

 

@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<args.length; i++){
if(null != args[i]){
errorLogInfoBuilder.Args("parameter"+i, args[i].toString());
}
}
}

try{
Object result = pjp.proceed();
return result;
}catch (ErrorCodeException exp1){
errorLogInfoBuilder.E(exp1).ErrorCode(exp1.getErrorCode()).build().logError();
return OperateResult.fail(exp1.getErrorCode());
}catch (Exception e){
errorLogInfoBuilder.E(e).ErrorCode(FrameworkErrorCode.INTERNAL_ERROR).build().logError();
return OperateResult.fail(FrameworkErrorCode.INTERNAL_ERROR);
}
}
}

@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);
}


}

自定义异常 状态码 以及Aop拦截Apect

标签:poi   return   format   hold   访问   inter   document   aspect   val   

原文地址:https://www.cnblogs.com/wwqwwq/p/10981392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!