You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@linkis.apache.org by pe...@apache.org on 2022/09/23 10:22:52 UTC

[incubator-linkis] branch dev-1.3.1 updated: errorcode code optimization (#3509)

This is an automated email from the ASF dual-hosted git repository.

peacewong pushed a commit to branch dev-1.3.1
in repository https://gitbox.apache.org/repos/asf/incubator-linkis.git


The following commit(s) were added to refs/heads/dev-1.3.1 by this push:
     new 764136a97 errorcode code optimization (#3509)
764136a97 is described below

commit 764136a97ffc450856f994a007525fb223a452aa
Author: 成彬彬 <10...@users.noreply.github.com>
AuthorDate: Fri Sep 23 18:22:47 2022 +0800

    errorcode code optimization (#3509)
---
 .../linkis-gateway-authentication-errorcode.md     | 12 ++++
 .../LinkisGwAuthenticationErrorCodeSummary.java    | 74 ++++++++++++++++++++++
 .../service/CachedTokenService.scala               | 27 +++++---
 3 files changed, 104 insertions(+), 9 deletions(-)

diff --git a/docs/errorcode/linkis-gateway-authentication-errorcode.md b/docs/errorcode/linkis-gateway-authentication-errorcode.md
new file mode 100644
index 000000000..e468b65ed
--- /dev/null
+++ b/docs/errorcode/linkis-gateway-authentication-errorcode.md
@@ -0,0 +1,12 @@
+## linkis-gateway-authentication  errorcode
+
+| 模块名(服务名) | 错误码  | 描述 | Exception Class|
+| -------- | -------- | ----- |-----|
+|linkis-gateway-authentication |15205|token is null!(令牌为空!)|LinkisGwAuthenticationErrorCodeSummary|
+|linkis-gateway-authentication |15200|Failed to load token from DB into cache!(无法将令牌从数据库加载到缓存中!)|LinkisGwAuthenticationErrorCodeSummary|
+|linkis-gateway-authentication |15201|Token is not valid or stale!(令牌无效或陈旧!)|LinkisGwAuthenticationErrorCodeSummary|
+|linkis-gateway-authentication |15202|Illegal TokenUser for Token!(代币非法用户!)|LinkisGwAuthenticationErrorCodeSummary|
+|linkis-gateway-authentication |15203|Illegal Host for Token!(Token非法主机!)|LinkisGwAuthenticationErrorCodeSummary|
+|linkis-gateway-authentication |15204|Invalid Token(令牌无效)|LinkisGwAuthenticationErrorCodeSummary|
+
+
diff --git a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-authentication/src/main/java/org/apache/linkis/gateway/authentication/errorcode/LinkisGwAuthenticationErrorCodeSummary.java b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-authentication/src/main/java/org/apache/linkis/gateway/authentication/errorcode/LinkisGwAuthenticationErrorCodeSummary.java
new file mode 100644
index 000000000..6519d2e27
--- /dev/null
+++ b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-authentication/src/main/java/org/apache/linkis/gateway/authentication/errorcode/LinkisGwAuthenticationErrorCodeSummary.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.linkis.gateway.authentication.errorcode;
+
+public enum LinkisGwAuthenticationErrorCodeSummary {
+  TOKEN_IS_NULL(15205, "token is null!(令牌为空!)", "token is null!(令牌为空!)"),
+  FAILED_TO_LOAD_TOKEN(
+      15200,
+      "Failed to load token from DB into cache!(无法将令牌从数据库加载到缓存中!)",
+      "Failed to load token from DB into cache!(无法将令牌从数据库加载到缓存中!)"),
+  TOKEN_VALID_OR_STALE(
+      15201, "Token is not valid or stale!(令牌无效或陈旧!)", "Token is not valid or stale!(令牌无效或陈旧!)"),
+  ILLEGAL_TOKENUSER(
+      15202, "Illegal TokenUser for Token!(代币非法用户!)", "Illegal TokenUser for Token!(代币非法用户!)"),
+  ILLEGAL_HOST(15203, "Illegal Host for Token!(Token非法主机!)", "Illegal Host for Token!(Token非法主机!)"),
+  INVALID_TOKEN(15204, "Invalid Token(令牌无效)", "Invalid Token(令牌无效)");
+
+  /** (errorCode)错误码 */
+  private int errorCode;
+  /** (errorDesc)错误描述 */
+  private String errorDesc;
+  /** Possible reasons for the error(错误可能出现的原因) */
+  private String comment;
+
+  LinkisGwAuthenticationErrorCodeSummary(int errorCode, String errorDesc, String comment) {
+    this.errorCode = errorCode;
+    this.errorDesc = errorDesc;
+    this.comment = comment;
+  }
+
+  public int getErrorCode() {
+    return errorCode;
+  }
+
+  public void setErrorCode(int errorCode) {
+    this.errorCode = errorCode;
+  }
+
+  public String getErrorDesc() {
+    return errorDesc;
+  }
+
+  public void setErrorDesc(String errorDesc) {
+    this.errorDesc = errorDesc;
+  }
+
+  public String getComment() {
+    return comment;
+  }
+
+  public void setComment(String comment) {
+    this.comment = comment;
+  }
+
+  @Override
+  public String toString() {
+    return "errorCode: " + this.errorCode + ", errorDesc:" + this.errorDesc;
+  }
+}
diff --git a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-authentication/src/main/scala/org/apache/linkis/gateway/authentication/service/CachedTokenService.scala b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-authentication/src/main/scala/org/apache/linkis/gateway/authentication/service/CachedTokenService.scala
index a314845c9..e839d0b5b 100644
--- a/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-authentication/src/main/scala/org/apache/linkis/gateway/authentication/service/CachedTokenService.scala
+++ b/linkis-spring-cloud-services/linkis-service-gateway/linkis-gateway-authentication/src/main/scala/org/apache/linkis/gateway/authentication/service/CachedTokenService.scala
@@ -17,14 +17,13 @@
 
 package org.apache.linkis.gateway.authentication.service
 
-import org.apache.linkis.common.exception.ErrorException
 import org.apache.linkis.common.utils.Utils
 import org.apache.linkis.gateway.authentication.bo.{Token, User}
-import org.apache.linkis.gateway.authentication.bo.Token
 import org.apache.linkis.gateway.authentication.bo.impl.TokenImpl
 import org.apache.linkis.gateway.authentication.conf.TokenConfiguration
 import org.apache.linkis.gateway.authentication.dao.TokenDao
 import org.apache.linkis.gateway.authentication.entity.TokenEntity
+import org.apache.linkis.gateway.authentication.errorcode.LinkisGwAuthenticationErrorCodeSummary._
 import org.apache.linkis.gateway.authentication.exception.{
   TokenAuthException,
   TokenNotExistException
@@ -54,7 +53,7 @@ class CachedTokenService extends TokenService {
         if (tokenEntity != null) {
           new TokenImpl().convertFrom(tokenEntity)
         } else {
-          throw new TokenNotExistException(15204, s"Invalid Token")
+          throw new TokenNotExistException(INVALID_TOKEN.getErrorCode, INVALID_TOKEN.getErrorDesc)
         }
       }
 
@@ -105,7 +104,7 @@ class CachedTokenService extends TokenService {
 
   private def loadTokenFromCache(tokenName: String): Token = {
     if (tokenName == null) {
-      throw new TokenAuthException(15205, "Token is null!")
+      throw new TokenAuthException(TOKEN_IS_NULL.getErrorCode, TOKEN_IS_NULL.getErrorDesc)
     }
     Utils.tryCatch(tokenCache.get(tokenName))(t =>
       t match {
@@ -113,9 +112,16 @@ class CachedTokenService extends TokenService {
           x.getCause match {
             case _: TokenNotExistException => null
             case _ =>
-              throw new TokenAuthException(15200, "Failed to load token from DB into cache!")
+              throw new TokenAuthException(
+                FAILED_TO_LOAD_TOKEN.getErrorCode,
+                FAILED_TO_LOAD_TOKEN.getErrorDesc
+              )
           }
-        case _ => throw new TokenAuthException(15200, "Failed to load token from DB into cache!")
+        case _ =>
+          throw new TokenAuthException(
+            FAILED_TO_LOAD_TOKEN.getErrorCode,
+            FAILED_TO_LOAD_TOKEN.getErrorDesc
+          )
       }
     )
   }
@@ -149,15 +155,18 @@ class CachedTokenService extends TokenService {
     var ok: Boolean = true
     if (!isTokenValid(tmpToken)) {
       ok = false
-      throw new TokenAuthException(15201, "Token is not valid or stale!")
+      throw new TokenAuthException(
+        TOKEN_VALID_OR_STALE.getErrorCode,
+        TOKEN_VALID_OR_STALE.getErrorDesc
+      )
     }
     if (!isTokenAcceptableWithUser(tmpToken, userName)) {
       ok = false
-      throw new TokenAuthException(15202, "Illegal TokenUser for Token!")
+      throw new TokenAuthException(ILLEGAL_TOKENUSER.getErrorCode, ILLEGAL_TOKENUSER.getErrorDesc)
     }
     if (!isTokenAcceptableWithHost(tmpToken, host)) {
       ok = false
-      throw new TokenAuthException(15203, "Illegal Host for Token!")
+      throw new TokenAuthException(ILLEGAL_HOST.getErrorCode, ILLEGAL_HOST.getErrorDesc)
     }
     ok
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@linkis.apache.org
For additional commands, e-mail: commits-help@linkis.apache.org