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

caogaofei pushed a commit to branch beyyes/fix_sonar
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 9409f963cdaf4b2680480411b62428a657c7f3a0
Author: Beyyes <[email protected]>
AuthorDate: Mon Jun 19 18:56:48 2023 +0800

    fix code smells in db/utils/ErrorHandlingUtils.java
---
 .../iotdb/db/client/DataNodeInternalClient.java    |  4 +-
 .../iotdb/db/query/control/SessionManager.java     |  4 +-
 .../service/thrift/impl/ClientRPCServiceImpl.java  | 48 +++++++++++-----------
 .../apache/iotdb/db/utils/ErrorHandlingUtils.java  | 24 ++++++-----
 4 files changed, 41 insertions(+), 39 deletions(-)

diff --git 
a/server/src/main/java/org/apache/iotdb/db/client/DataNodeInternalClient.java 
b/server/src/main/java/org/apache/iotdb/db/client/DataNodeInternalClient.java
index 272bcf776aa..2ac0cc86329 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/client/DataNodeInternalClient.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/client/DataNodeInternalClient.java
@@ -40,7 +40,7 @@ import org.apache.iotdb.rpc.TSStatusCode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static 
org.apache.iotdb.db.utils.ErrorHandlingUtils.onNPEOrUnexpectedException;
+import static 
org.apache.iotdb.db.utils.ErrorHandlingUtils.onNpeOrUnexpectedException;
 
 public class DataNodeInternalClient {
 
@@ -93,7 +93,7 @@ public class DataNodeInternalClient {
               SCHEMA_FETCHER);
       return result.status;
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.INSERT_TABLETS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     }
   }
diff --git 
a/server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java 
b/server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java
index 005d6f401ae..a06299bacaa 100644
--- a/server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java
+++ b/server/src/main/java/org/apache/iotdb/db/query/control/SessionManager.java
@@ -57,7 +57,7 @@ import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 
-import static 
org.apache.iotdb.db.utils.ErrorHandlingUtils.onNPEOrUnexpectedException;
+import static 
org.apache.iotdb.db.utils.ErrorHandlingUtils.onNpeOrUnexpectedException;
 
 public class SessionManager implements SessionManagerMBean {
   private static final Logger LOGGER = 
LoggerFactory.getLogger(SessionManager.class);
@@ -219,7 +219,7 @@ public class SessionManager implements SessionManagerMBean {
             TSStatusCode.CLOSE_OPERATION_ERROR, "statement id not set by 
client.");
       }
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.CLOSE_OPERATION, 
TSStatusCode.CLOSE_OPERATION_ERROR);
     }
   }
diff --git 
a/server/src/main/java/org/apache/iotdb/db/service/thrift/impl/ClientRPCServiceImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/service/thrift/impl/ClientRPCServiceImpl.java
index 8a1f9e323d9..463f0db7974 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/service/thrift/impl/ClientRPCServiceImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/service/thrift/impl/ClientRPCServiceImpl.java
@@ -139,7 +139,7 @@ import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 
 import static org.apache.iotdb.db.utils.ErrorHandlingUtils.onIoTDBException;
-import static 
org.apache.iotdb.db.utils.ErrorHandlingUtils.onNPEOrUnexpectedException;
+import static 
org.apache.iotdb.db.utils.ErrorHandlingUtils.onNpeOrUnexpectedException;
 import static org.apache.iotdb.db.utils.ErrorHandlingUtils.onQueryException;
 
 public class ClientRPCServiceImpl implements IClientRPCServiceWithHandler {
@@ -707,7 +707,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
           zoneId != null ? zoneId.toString() : "Unknown time zone");
     } catch (Exception e) {
       return new TSGetTimeZoneResp(
-          onNPEOrUnexpectedException(
+          onNpeOrUnexpectedException(
               e, OperationType.GET_TIME_ZONE, 
TSStatusCode.GENERATE_TIME_ZONE_ERROR),
           "Unknown time zone");
     }
@@ -719,7 +719,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       SESSION_MANAGER.getCurrSession().setZoneId(ZoneId.of(req.timeZone));
       return RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS);
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.SET_TIME_ZONE, TSStatusCode.SET_TIME_ZONE_ERROR);
     }
   }
@@ -779,7 +779,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.SET_STORAGE_GROUP, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.SET_STORAGE_GROUP, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     }
   }
@@ -820,7 +820,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.CREATE_TIMESERIES, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.CREATE_TIMESERIES, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -870,7 +870,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.CREATE_ALIGNED_TIMESERIES, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.CREATE_ALIGNED_TIMESERIES, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -919,7 +919,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.CREATE_MULTI_TIMESERIES, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.CREATE_MULTI_TIMESERIES, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -959,7 +959,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.DELETE_TIMESERIES, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.DELETE_TIMESERIES, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -1002,7 +1002,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.DELETE_STORAGE_GROUPS, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.DELETE_STORAGE_GROUPS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -1223,7 +1223,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.INSERT_RECORDS, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.INSERT_RECORDS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       addStatementExecutionLatency(
@@ -1290,7 +1290,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.INSERT_RECORDS_OF_ONE_DEVICE, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.INSERT_RECORDS_OF_ONE_DEVICE, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       addStatementExecutionLatency(
@@ -1359,7 +1359,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return onIoTDBException(
           e, OperationType.INSERT_STRING_RECORDS_OF_ONE_DEVICE, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e,
           OperationType.INSERT_STRING_RECORDS_OF_ONE_DEVICE,
           TSStatusCode.EXECUTE_STATEMENT_ERROR);
@@ -1427,7 +1427,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.INSERT_RECORD, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.INSERT_RECORD, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       addStatementExecutionLatency(
@@ -1484,7 +1484,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.INSERT_TABLETS, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.INSERT_TABLETS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       addStatementExecutionLatency(
@@ -1540,7 +1540,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.INSERT_TABLET, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.INSERT_TABLET, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       addStatementExecutionLatency(
@@ -1605,7 +1605,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.INSERT_STRING_RECORDS, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.INSERT_STRING_RECORDS, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       addStatementExecutionLatency(
@@ -1691,7 +1691,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.DELETE_DATA, e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.DELETE_DATA, TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -1755,7 +1755,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.CREATE_SCHEMA_TEMPLATE, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.CREATE_SCHEMA_TEMPLATE, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -1816,7 +1816,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       return executeTemplateQueryStatement(statement, req, resp);
     } catch (Exception e) {
       resp.setStatus(
-          onNPEOrUnexpectedException(
+          onNpeOrUnexpectedException(
               e, OperationType.EXECUTE_QUERY_STATEMENT, 
TSStatusCode.EXECUTE_STATEMENT_ERROR));
       return resp;
     } finally {
@@ -1936,7 +1936,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IllegalPathException e) {
       return onIoTDBException(e, OperationType.EXECUTE_STATEMENT, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.EXECUTE_STATEMENT, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -1984,7 +1984,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IllegalPathException e) {
       return onIoTDBException(e, OperationType.EXECUTE_STATEMENT, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.EXECUTE_STATEMENT, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -2027,7 +2027,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
 
       return result.status;
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.EXECUTE_STATEMENT, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -2073,7 +2073,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.EXECUTE_STATEMENT, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.EXECUTE_STATEMENT, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       SESSION_MANAGER.updateIdleTime();
@@ -2163,7 +2163,7 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     } catch (IoTDBException e) {
       return onIoTDBException(e, OperationType.INSERT_STRING_RECORD, 
e.getErrorCode());
     } catch (Exception e) {
-      return onNPEOrUnexpectedException(
+      return onNpeOrUnexpectedException(
           e, OperationType.INSERT_STRING_RECORD, 
TSStatusCode.EXECUTE_STATEMENT_ERROR);
     } finally {
       addStatementExecutionLatency(
diff --git 
a/server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java 
b/server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
index 067eef1bec6..6000f1f1779 100644
--- a/server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
+++ b/server/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
@@ -16,6 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 package org.apache.iotdb.db.utils;
 
 import org.apache.iotdb.common.rpc.thrift.TSStatus;
@@ -41,31 +42,32 @@ import java.util.concurrent.ExecutionException;
 
 public class ErrorHandlingUtils {
 
+  private ErrorHandlingUtils() {}
+
   private static final Logger LOGGER = 
LoggerFactory.getLogger(ErrorHandlingUtils.class);
-  private static final Logger DETAILED_FAILURE_QUERY_TRACE_LOGGER =
-      LoggerFactory.getLogger("DETAILED_FAILURE_QUERY_TRACE");
 
   private static final String INFO_PARSING_SQL_ERROR =
       "Error occurred while parsing SQL to physical plan: ";
-  private static final String INFO_CHECK_METADATA_ERROR = "Check metadata 
error: ";
   private static final String INFO_QUERY_PROCESS_ERROR = "Error occurred in 
query process: ";
   private static final String INFO_NOT_ALLOWED_IN_BATCH_ERROR =
       "The query statement is not allowed in batch: ";
 
-  public static TSStatus onNPEOrUnexpectedException(
+  private static final String ERROR_OPERATION_LOG = "Status code: {}, 
operation: {} failed";
+
+  public static TSStatus onNpeOrUnexpectedException(
       Exception e, String operation, TSStatusCode statusCode) {
     String message = String.format("[%s] Exception occurred: %s failed. ", 
statusCode, operation);
     if (e instanceof IOException || e instanceof NullPointerException) {
-      LOGGER.error("Status code: " + statusCode + ", operation: " + operation 
+ " failed", e);
+      LOGGER.error(ERROR_OPERATION_LOG, statusCode, operation, e);
     } else {
-      LOGGER.warn("Status code: " + statusCode + ", operation: " + operation + 
" failed", e);
+      LOGGER.warn(ERROR_OPERATION_LOG, statusCode, operation, e);
     }
     return RpcUtils.getStatus(statusCode, message + e.getMessage());
   }
 
-  public static TSStatus onNPEOrUnexpectedException(
+  public static TSStatus onNpeOrUnexpectedException(
       Exception e, OperationType operation, TSStatusCode statusCode) {
-    return onNPEOrUnexpectedException(e, operation.getName(), statusCode);
+    return onNpeOrUnexpectedException(e, operation.getName(), statusCode);
   }
 
   public static Throwable getRootCause(Throwable e) {
@@ -91,7 +93,7 @@ public class ErrorHandlingUtils {
       }
       return status;
     } else {
-      return onNPEOrUnexpectedException(e, operation, statusCode);
+      return onNpeOrUnexpectedException(e, operation, statusCode);
     }
   }
 
@@ -142,7 +144,7 @@ public class ErrorHandlingUtils {
     TSStatus status = tryCatchNonQueryException(e);
     return status != null
         ? status
-        : onNPEOrUnexpectedException(e, operation, 
TSStatusCode.INTERNAL_SERVER_ERROR);
+        : onNpeOrUnexpectedException(e, operation, 
TSStatusCode.INTERNAL_SERVER_ERROR);
   }
 
   public static TSStatus onNonQueryException(Exception e, OperationType 
operation) {
@@ -177,7 +179,7 @@ public class ErrorHandlingUtils {
     String message =
         String.format(
             "[%s] Exception occurred: %s failed. %s", statusCode, operation, 
e.getMessage());
-    LOGGER.warn("Status code: " + statusCode + ", operation: " + operation + " 
failed", e);
+    LOGGER.warn(ERROR_OPERATION_LOG, statusCode, operation, e);
     return RpcUtils.getStatus(errorCode, message);
   }
 

Reply via email to