diff --git a/austin-support/src/main/java/com/java3y/austin/support/exception/ProcessException.java b/austin-support/src/main/java/com/java3y/austin/support/exception/ProcessException.java new file mode 100644 index 0000000..129cfab --- /dev/null +++ b/austin-support/src/main/java/com/java3y/austin/support/exception/ProcessException.java @@ -0,0 +1,39 @@ +package com.java3y.austin.support.exception; + +import com.java3y.austin.common.enums.RespStatusEnum; +import com.java3y.austin.support.pipeline.ProcessContext; + +/** + * @author SamLee + * @since 2022-03-29 + */ +public class ProcessException extends RuntimeException { + + /** + * 流程处理上下文 + */ + private final ProcessContext processContext; + + public ProcessException(ProcessContext processContext) { + super(); + this.processContext = processContext; + } + + public ProcessException(ProcessContext processContext, Throwable cause) { + super(cause); + this.processContext = processContext; + } + + @Override + public String getMessage() { + if (this.processContext != null) { + return this.processContext.getResponse().getMsg(); + } else { + return RespStatusEnum.CONTEXT_IS_NULL.getMsg(); + } + } + + public ProcessContext getProcessContext() { + return processContext; + } +} diff --git a/austin-support/src/main/java/com/java3y/austin/support/pipeline/ProcessController.java b/austin-support/src/main/java/com/java3y/austin/support/pipeline/ProcessController.java index 977178b..82fdddb 100644 --- a/austin-support/src/main/java/com/java3y/austin/support/pipeline/ProcessController.java +++ b/austin-support/src/main/java/com/java3y/austin/support/pipeline/ProcessController.java @@ -5,6 +5,7 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.java3y.austin.common.enums.RespStatusEnum; import com.java3y.austin.common.vo.BasicResultVO; +import com.java3y.austin.support.exception.ProcessException; import lombok.Data; import lombok.extern.slf4j.Slf4j; @@ -34,10 +35,13 @@ public class ProcessController { */ public ProcessContext process(ProcessContext context) { - /** - * 前置检查 - */ - if (!preCheck(context)) { + try { + /** + * 前置检查 + */ + preCheck(context); + } catch(ProcessException e) { + context = e.getProcessContext(); return context; } @@ -55,36 +59,40 @@ public class ProcessController { } - private Boolean preCheck(ProcessContext context) { + /** + * 执行前检查,出错则抛出异常 + * @param context 执行上下文 + * @throws ProcessException 异常信息 + */ + private void preCheck(ProcessContext context) throws ProcessException { // 上下文 if (context == null) { context = new ProcessContext(); context.setResponse(BasicResultVO.fail(RespStatusEnum.CONTEXT_IS_NULL)); - return false; + throw new ProcessException(context); } // 业务代码 String businessCode = context.getCode(); if (StrUtil.isBlank(businessCode)) { context.setResponse(BasicResultVO.fail(RespStatusEnum.BUSINESS_CODE_IS_NULL)); - return false; + throw new ProcessException(context); } // 执行模板 ProcessTemplate processTemplate = templateConfig.get(businessCode); if (processTemplate == null) { context.setResponse(BasicResultVO.fail(RespStatusEnum.PROCESS_TEMPLATE_IS_NULL)); - return false; + throw new ProcessException(context); } // 执行模板列表 List processList = processTemplate.getProcessList(); if (CollUtil.isEmpty(processList)) { context.setResponse(BasicResultVO.fail(RespStatusEnum.PROCESS_LIST_IS_NULL)); - return false; + throw new ProcessException(context); } - return true; }