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

loogn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/geaflow.git


The following commit(s) were added to refs/heads/master by this push:
     new b3b4c5073 [ISSUE701]Use classifier (#720)
b3b4c5073 is described below

commit b3b4c5073392c52eb0ed5877d9a75fbbed406368
Author: yazong <[email protected]>
AuthorDate: Fri Dec 26 09:40:34 2025 +0800

    [ISSUE701]Use classifier (#720)
    
    Co-authored-by: liliya <[email protected]>
---
 .../util/exception/GeaflowExceptionClassifier.java | 86 ++++++++++++++++++++++
 .../console/web/api/ErrorApiCorsConfigurer.java    | 44 +++++++++++
 .../geaflow/console/web/api/ErrorApiResponse.java  | 64 +++-------------
 3 files changed, 140 insertions(+), 54 deletions(-)

diff --git 
a/geaflow-console/app/common/util/src/main/java/org/apache/geaflow/console/common/util/exception/GeaflowExceptionClassifier.java
 
b/geaflow-console/app/common/util/src/main/java/org/apache/geaflow/console/common/util/exception/GeaflowExceptionClassifier.java
new file mode 100644
index 000000000..312f4b7dd
--- /dev/null
+++ 
b/geaflow-console/app/common/util/src/main/java/org/apache/geaflow/console/common/util/exception/GeaflowExceptionClassifier.java
@@ -0,0 +1,86 @@
+/*
+ * 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.geaflow.console.common.util.exception;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.geaflow.console.common.util.type.GeaflowApiResponseCode;
+
+public class GeaflowExceptionClassifier {
+
+    public GeaflowExceptionClassificationResult classify(Throwable error) {
+        GeaflowApiResponseCode code;
+        String message = error.getMessage();
+
+        if (StringUtils.isBlank(message)) {
+            message = error.getClass().getSimpleName();
+        }
+
+        if (error instanceof GeaflowSecurityException) {
+            code = GeaflowApiResponseCode.FORBIDDEN;
+
+        } else if (error instanceof GeaflowIllegalException) {
+            code = GeaflowApiResponseCode.ILLEGAL;
+
+        } else if (error instanceof GeaflowCompileException) {
+            code = GeaflowApiResponseCode.ERROR;
+            message = ((GeaflowCompileException) error).getDisplayMessage();
+
+        } else if (error instanceof GeaflowException) {
+            code = GeaflowApiResponseCode.ERROR;
+
+        } else if (error instanceof IllegalArgumentException) {
+            code = GeaflowApiResponseCode.ILLEGAL;
+
+        } else if (error instanceof NullPointerException) {
+            code = GeaflowApiResponseCode.ERROR;
+
+        } else {
+            code = GeaflowApiResponseCode.FAIL;
+            // Traverse to root cause for better error message
+            while (error.getCause() != null) {
+                error = error.getCause();
+            }
+            message = error.getMessage();
+            if (StringUtils.isBlank(message)) {
+                message = error.getClass().getSimpleName();
+            }
+        }
+
+        return new GeaflowExceptionClassificationResult(code, message);
+    }
+
+    public static class GeaflowExceptionClassificationResult {
+        private final GeaflowApiResponseCode code;
+        private final String message;
+
+        public GeaflowExceptionClassificationResult(GeaflowApiResponseCode 
code, String message) {
+            this.code = code;
+            this.message = message;
+        }
+
+        public GeaflowApiResponseCode getCode() {
+            return code;
+        }
+
+        public String getMessage() {
+            return message;
+        }
+    }
+}
diff --git 
a/geaflow-console/app/web/src/main/java/org/apache/geaflow/console/web/api/ErrorApiCorsConfigurer.java
 
b/geaflow-console/app/web/src/main/java/org/apache/geaflow/console/web/api/ErrorApiCorsConfigurer.java
new file mode 100644
index 000000000..2e7244ea3
--- /dev/null
+++ 
b/geaflow-console/app/web/src/main/java/org/apache/geaflow/console/web/api/ErrorApiCorsConfigurer.java
@@ -0,0 +1,44 @@
+/*
+ * 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.geaflow.console.web.api;
+
+import javax.servlet.http.HttpServletResponse;
+import org.apache.geaflow.console.common.util.context.ContextHolder;
+import org.springframework.http.HttpHeaders;
+
+public class ErrorApiCorsConfigurer {
+
+    private ErrorApiCorsConfigurer() {
+    }
+
+    public static void configure(HttpServletResponse response) {
+        String originKey = HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN;
+        String credentialsKey = HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS;
+        String headersKey = HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS;
+        String methodsKey = HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS;
+        String ageKey = HttpHeaders.ACCESS_CONTROL_MAX_AGE;
+
+        response.setHeader(originKey, 
ContextHolder.get().getRequest().getHeader(HttpHeaders.ORIGIN));
+        response.setHeader(methodsKey, 
"OPTIONS,HEAD,GET,POST,PUT,PATCH,DELETE,TRACE");
+        response.setHeader(headersKey, 
"Origin,X-Requested-With,Content-Type,Accept,geaflow-token,geaflow-task-token");
+        response.setHeader(credentialsKey, "true");
+        response.setHeader(ageKey, "3600");
+    }
+}
diff --git 
a/geaflow-console/app/web/src/main/java/org/apache/geaflow/console/web/api/ErrorApiResponse.java
 
b/geaflow-console/app/web/src/main/java/org/apache/geaflow/console/web/api/ErrorApiResponse.java
index 0387ce207..4c97371e3 100644
--- 
a/geaflow-console/app/web/src/main/java/org/apache/geaflow/console/web/api/ErrorApiResponse.java
+++ 
b/geaflow-console/app/web/src/main/java/org/apache/geaflow/console/web/api/ErrorApiResponse.java
@@ -21,18 +21,14 @@ package org.apache.geaflow.console.web.api;
 
 import javax.servlet.http.HttpServletResponse;
 import lombok.Getter;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.geaflow.console.common.util.context.ContextHolder;
-import 
org.apache.geaflow.console.common.util.exception.GeaflowCompileException;
-import org.apache.geaflow.console.common.util.exception.GeaflowException;
-import 
org.apache.geaflow.console.common.util.exception.GeaflowIllegalException;
-import 
org.apache.geaflow.console.common.util.exception.GeaflowSecurityException;
-import org.apache.geaflow.console.common.util.type.GeaflowApiResponseCode;
-import org.springframework.http.HttpHeaders;
+import 
org.apache.geaflow.console.common.util.exception.GeaflowExceptionClassifier;
+import 
org.apache.geaflow.console.common.util.exception.GeaflowExceptionClassifier.GeaflowExceptionClassificationResult;
 
 @Getter
 public class ErrorApiResponse<T> extends GeaflowApiResponse<T> {
 
+    private static final GeaflowExceptionClassifier EXCEPTION_CLASSIFIER = new 
GeaflowExceptionClassifier();
+
     private final GeaflowApiRequest<?> request;
 
     private final String message;
@@ -40,59 +36,19 @@ public class ErrorApiResponse<T> extends 
GeaflowApiResponse<T> {
     protected ErrorApiResponse(Throwable error) {
         super(false);
 
-        String message = error.getMessage();
-        if (StringUtils.isBlank(message)) {
-            message = error.getClass().getSimpleName();
-        }
-
-        if (error instanceof GeaflowSecurityException) {
-            this.code = GeaflowApiResponseCode.FORBIDDEN;
-
-        } else if (error instanceof GeaflowIllegalException) {
-            this.code = GeaflowApiResponseCode.ILLEGAL;
-
-        } else if (error instanceof GeaflowCompileException) {
-            this.code = GeaflowApiResponseCode.ERROR;
-            message = ((GeaflowCompileException) error).getDisplayMessage();
-
-        } else if (error instanceof GeaflowException) {
-            this.code = GeaflowApiResponseCode.ERROR;
-
-        } else if (error instanceof IllegalArgumentException) {
-            this.code = GeaflowApiResponseCode.ILLEGAL;
-
-        } else if (error instanceof NullPointerException) {
-            this.code = GeaflowApiResponseCode.ERROR;
-
-        } else {
-            this.code = GeaflowApiResponseCode.FAIL;
-            while (error.getCause() != null) {
-                error = error.getCause();
-            }
-        }
+        // Use classifier to classify the exception
+        GeaflowExceptionClassificationResult result = 
EXCEPTION_CLASSIFIER.classify(error);
+        this.code = result.getCode();
+        this.message = result.getMessage();
 
         this.request = GeaflowApiRequest.currentRequest();
-        this.message = message;
     }
 
     @Override
     public void write(HttpServletResponse response) {
         response.reset();
-        configCors(response);
+        // Use centralized CORS configuration
+        ErrorApiCorsConfigurer.configure(response);
         super.write(response);
     }
-
-    private void configCors(HttpServletResponse response) {
-        String originKey = HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN;
-        String credentialsKey = HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS;
-        String headersKey = HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS;
-        String methodsKey = HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS;
-        String ageKey = HttpHeaders.ACCESS_CONTROL_MAX_AGE;
-
-        response.setHeader(originKey, 
ContextHolder.get().getRequest().getHeader(HttpHeaders.ORIGIN));
-        response.setHeader(methodsKey, 
"OPTIONS,HEAD,GET,POST,PUT,PATCH,DELETE,TRACE");
-        response.setHeader(headersKey, 
"Origin,X-Requested-With,Content-Type,Accept,geaflow-token,geaflow-task-token");
-        response.setHeader(credentialsKey, "true");
-        response.setHeader(ageKey, "3600");
-    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to