You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampark.apache.org by be...@apache.org on 2022/10/12 06:24:29 UTC

[incubator-streampark] branch dev updated: [Improve] Refactor the AlertException (#1811)

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

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


The following commit(s) were added to refs/heads/dev by this push:
     new 201d20b80 [Improve] Refactor the AlertException (#1811)
201d20b80 is described below

commit 201d20b805c5df09d00210c9716168e23ca91dff
Author: 1996fanrui <19...@gmail.com>
AuthorDate: Wed Oct 12 14:24:22 2022 +0800

    [Improve] Refactor the AlertException (#1811)
---
 ...ertException.java => AbstractApiException.java} | 23 +++++++++++++--------
 .../console/base/exception/ApiAlertException.java  | 10 +++++----
 .../console/base/exception/ApiDetailException.java | 13 ++++++++----
 .../base/handler/GlobalExceptionHandler.java       | 24 ++++------------------
 4 files changed, 33 insertions(+), 37 deletions(-)

diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiAlertException.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/AbstractApiException.java
similarity index 61%
copy from streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiAlertException.java
copy to streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/AbstractApiException.java
index 9c7eff6ca..a9cbc33c4 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiAlertException.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/AbstractApiException.java
@@ -19,25 +19,30 @@ package org.apache.streampark.console.base.exception;
 
 /**
  * <pre>
- * An exception message that needs to be notified to front-end,
- * usually a <strong>simple</strong>, clear</strong> message, e.g:
- * <p>1. Username already exists</p>
- * <p>2. No permission, please contact the administrator</p>
- * ...
+ * An exception message that needs to be notified to front-end.
  * </pre>
  */
-public class ApiAlertException extends RuntimeException {
+public abstract class AbstractApiException extends RuntimeException {
 
-    public ApiAlertException(String message) {
+    private final long responseCode;
+
+    protected AbstractApiException(String message, long responseCode) {
         super(message);
+        this.responseCode = responseCode;
     }
 
-    public ApiAlertException(Throwable cause) {
+    protected AbstractApiException(Throwable cause, long responseCode) {
         super(cause);
+        this.responseCode = responseCode;
     }
 
-    public ApiAlertException(String message, Throwable cause) {
+    protected AbstractApiException(String message, Throwable cause, long responseCode) {
         super(message, cause);
+        this.responseCode = responseCode;
+    }
+
+    public long getResponseCode() {
+        return responseCode;
     }
 
 }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiAlertException.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiAlertException.java
index 9c7eff6ca..1f2a21db9 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiAlertException.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiAlertException.java
@@ -17,6 +17,8 @@
 
 package org.apache.streampark.console.base.exception;
 
+import org.apache.streampark.console.base.domain.ResponseCode;
+
 /**
  * <pre>
  * An exception message that needs to be notified to front-end,
@@ -26,18 +28,18 @@ package org.apache.streampark.console.base.exception;
  * ...
  * </pre>
  */
-public class ApiAlertException extends RuntimeException {
+public class ApiAlertException extends AbstractApiException {
 
     public ApiAlertException(String message) {
-        super(message);
+        super(message, ResponseCode.CODE_FAIL_ALERT);
     }
 
     public ApiAlertException(Throwable cause) {
-        super(cause);
+        super(cause, ResponseCode.CODE_FAIL_ALERT);
     }
 
     public ApiAlertException(String message, Throwable cause) {
-        super(message, cause);
+        super(message, cause, ResponseCode.CODE_FAIL_ALERT);
     }
 
 }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiDetailException.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiDetailException.java
index 2d2eb168d..d54a6de44 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiDetailException.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/ApiDetailException.java
@@ -18,6 +18,7 @@
 package org.apache.streampark.console.base.exception;
 
 import org.apache.streampark.common.util.ExceptionUtils;
+import org.apache.streampark.console.base.domain.ResponseCode;
 
 /**
  * <pre>
@@ -27,18 +28,22 @@ import org.apache.streampark.common.util.ExceptionUtils;
  * <p>1. Failed to start job, need to display the exception(stackTrace info) to front-end</p>
  * </pre>
  */
-public class ApiDetailException extends RuntimeException {
+public class ApiDetailException extends AbstractApiException {
 
     public ApiDetailException(String message) {
-        super(message);
+        super(message, ResponseCode.CODE_FAIL_DETAIL);
     }
 
     public ApiDetailException(Throwable cause) {
-        super(ExceptionUtils.stringifyException(cause));
+        super(ExceptionUtils.stringifyException(cause), ResponseCode.CODE_FAIL_DETAIL);
     }
 
     public ApiDetailException(String message, Throwable cause) {
-        super(message + ExceptionUtils.stringifyException(cause));
+        super(message + ExceptionUtils.stringifyException(cause), ResponseCode.CODE_FAIL_DETAIL);
     }
 
+    @Override
+    public String getMessage() {
+        return "Detail exception: \n" + super.getMessage();
+    }
 }
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/handler/GlobalExceptionHandler.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/handler/GlobalExceptionHandler.java
index 55bc2158f..685f09c61 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/handler/GlobalExceptionHandler.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/handler/GlobalExceptionHandler.java
@@ -19,9 +19,7 @@ package org.apache.streampark.console.base.handler;
 
 import org.apache.streampark.console.base.domain.ResponseCode;
 import org.apache.streampark.console.base.domain.RestResponse;
-import org.apache.streampark.console.base.exception.ApiAlertException;
-import org.apache.streampark.console.base.exception.ApiDetailException;
-import org.apache.streampark.console.base.exception.InternalException;
+import org.apache.streampark.console.base.exception.AbstractApiException;
 
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import lombok.extern.slf4j.Slf4j;
@@ -56,13 +54,6 @@ public class GlobalExceptionHandler {
         return RestResponse.fail("internal server error: " + e.getMessage(), ResponseCode.CODE_FAIL);
     }
 
-    @ExceptionHandler(value = InternalException.class)
-    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-    public RestResponse handleParamsInvalidException(InternalException e) {
-        log.info("Internal server error:{}", e.getMessage());
-        return RestResponse.fail("internal server error: " + e.getMessage(), ResponseCode.CODE_FAIL);
-    }
-
     @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
     public RestResponse handleException(HttpRequestMethodNotSupportedException e) {
@@ -70,18 +61,11 @@ public class GlobalExceptionHandler {
         return RestResponse.fail("not supported request method,exception:" + e.getMessage(), ResponseCode.CODE_FAIL);
     }
 
-    @ExceptionHandler(value = ApiAlertException.class)
+    @ExceptionHandler(value = AbstractApiException.class)
     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-    public RestResponse handleException(ApiAlertException e) {
+    public RestResponse handleException(AbstractApiException e) {
         log.info("api exception:{}", e.getMessage());
-        return RestResponse.fail(e.getMessage(), ResponseCode.CODE_FAIL_ALERT);
-    }
-
-    @ExceptionHandler(value = ApiDetailException.class)
-    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-    public RestResponse handleException(ApiDetailException e) {
-        log.info("detail exception:{}", e.getMessage());
-        return RestResponse.fail("exception detail:\n" + e.getMessage(), ResponseCode.CODE_FAIL_DETAIL);
+        return RestResponse.fail(e.getMessage(), e.getResponseCode());
     }