自定义异常 状态码 以及Aop拦截Apect
Posted wwqwwq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义异常 状态码 以及Aop拦截Apect相关的知识,希望对你有一定的参考价值。
@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的主要内容,如果未能解决你的问题,请参考以下文章
Springboot+自定义注解+自定义AOP前置增强+自定义异常+自定义异常捕获