Browse Source

参数错误 不再重试

supeng 3 months ago
parent
commit
3e52e4514d

+ 36 - 0
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/common/enums/AigcServerExceptionEnum.java

@@ -0,0 +1,36 @@
+package com.tzld.piaoquan.content.understanding.common.enums;
+
+/**
+ * 异常
+ *
+ * @author supeng
+ * @date 2020/08/31
+ */
+public enum AigcServerExceptionEnum {
+
+    SYSTEM_ERROR(-111, "系统错误"),
+    DATA_ERROR(-222, "数据异常,请联系管理员"),
+    USER_IS_UNVALID(2, "用户操作鉴权不通过"),
+    PARAM_ERROR(3, "参数不对"),
+    FLOW_CONTROL(-777, "接口被限流"),
+    TOKEN_NOTEXIST(-994, "访问凭证不存在"),
+
+	;
+
+    private int code;
+    private String msg;
+
+    public int getCode() {
+        return code;
+    }
+
+
+    public String getMsg() {
+        return msg;
+    }
+
+    AigcServerExceptionEnum(int code, String msg) {
+        this.code = code;
+        this.msg = msg;
+    }
+}

+ 15 - 0
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/common/exception/StopRetryException.java

@@ -0,0 +1,15 @@
+package com.tzld.piaoquan.content.understanding.common.exception;
+
+/**
+ * 不在重试
+ *
+ * @author supeng
+ * @date 2020/08/19
+ */
+public class StopRetryException extends RuntimeException {
+
+    public StopRetryException(String message) {
+        super(message);
+    }
+
+}

+ 4 - 2
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/service/impl/GeminiGenerateContentAction.java

@@ -3,11 +3,12 @@ package com.tzld.piaoquan.content.understanding.service.impl;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.TypeReference;
 import com.tzld.piaoquan.content.understanding.common.base.CommonResponse;
+import com.tzld.piaoquan.content.understanding.common.enums.AigcServerExceptionEnum;
 import com.tzld.piaoquan.content.understanding.common.enums.ContentTypeEnum;
+import com.tzld.piaoquan.content.understanding.common.exception.StopRetryException;
 import com.tzld.piaoquan.content.understanding.model.param.ActionParam;
 import com.tzld.piaoquan.content.understanding.model.param.GeminiParam;
 import com.tzld.piaoquan.content.understanding.service.Action;
-import com.tzld.piaoquan.content.understanding.service.ContentService;
 import com.tzld.piaoquan.content.understanding.util.HttpClientUtil;
 import com.tzld.piaoquan.content.understanding.util.HttpPoolClient;
 import com.tzld.piaoquan.content.understanding.util.RedisUtil;
@@ -21,7 +22,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
-import java.util.concurrent.ThreadLocalRandom;
 
 /**
  * @author supeng
@@ -81,6 +81,8 @@ public class GeminiGenerateContentAction implements Action {
                         //过滤一些特殊格式
                         content = content.replace("```json", "").replace("```", "");
                         return content;
+                    } else if (AigcServerExceptionEnum.PARAM_ERROR.getCode() == commonResponse.getCode()) {
+                        throw new StopRetryException("geminiGenerateContentAction request params error geminiParam = " + JSON.toJSONString(geminiParam));
                     } else {
                         int index1 = getIndex(size);
                         String apiKey1 = apiKeyList.get(index1);

+ 5 - 0
content-understanding-core/src/main/java/com/tzld/piaoquan/content/understanding/util/RetryUtil.java

@@ -1,5 +1,6 @@
 package com.tzld.piaoquan.content.understanding.util;
 
+import com.tzld.piaoquan.content.understanding.common.exception.StopRetryException;
 import lombok.extern.slf4j.Slf4j;
 
 import java.util.Objects;
@@ -55,6 +56,10 @@ public class RetryUtil {
                 return action.get();
             } catch (Exception e) {
                 log.error("{} failed on attempt {}: {}", actionName, i, e.getMessage(), e);
+                if (e instanceof StopRetryException) {
+                    log.error("{} failed on attempt {} due to StopRetryException. Stopping retries.", actionName, i);
+                    throw new RuntimeException(actionName + " failed due to Stopping retries.", e);
+                }
                 if (i < maxRetries) {
                     if (Objects.nonNull(retryIntervalProvider) && Objects.nonNull(retryIntervalProvider.apply(i))) {
                         long interval = retryIntervalProvider.apply(i);