From 57a57ace77784b515b7fea86e1f55a556e58bc18 Mon Sep 17 00:00:00 2001 From: v-zhangjc9 Date: Tue, 22 Oct 2024 17:08:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(sync):=20=E5=A2=9E=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../functions/Record2RowDataFunction.java | 3 ++- .../sync/functions/type/TypeConverterV2.java | 7 +++-- .../service/sync/utils/ExceptionUtils.java | 26 +++++++++++++++++++ .../service/sync/utils/ZkUtils.java | 9 +++---- 4 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/utils/ExceptionUtils.java diff --git a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/Record2RowDataFunction.java b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/Record2RowDataFunction.java index 673e90b..bf78311 100644 --- a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/Record2RowDataFunction.java +++ b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/Record2RowDataFunction.java @@ -11,6 +11,7 @@ import com.lanyuanxiaoyao.service.common.utils.RecordHelper; import com.lanyuanxiaoyao.service.common.utils.TableMetaHelper; import com.lanyuanxiaoyao.service.sync.configuration.GlobalConfiguration; import com.lanyuanxiaoyao.service.sync.functions.type.TypeConverter; +import com.lanyuanxiaoyao.service.sync.utils.ExceptionUtils; import com.lanyuanxiaoyao.service.sync.utils.JacksonUtils; import com.lanyuanxiaoyao.service.sync.utils.MetricsUtils; import com.lanyuanxiaoyao.service.sync.utils.StatusUtils; @@ -94,7 +95,7 @@ public class Record2RowDataFunction extends RichMapFunction current = RecordHelper.getCurrentStatement(record); if (Objects.isNull(current)) { logger.error("Record: {}", mapper.writeValueAsString(record)); - throw new RuntimeException("Current cannot be null"); + return ExceptionUtils.throwAndPrint(logger, "Current cannot be null"); } // 如果 update 改变了过滤字段的值也需要先删除 diff --git a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/type/TypeConverterV2.java b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/type/TypeConverterV2.java index 17ee518..49a1063 100644 --- a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/type/TypeConverterV2.java +++ b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/type/TypeConverterV2.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil; import com.lanyuanxiaoyao.service.common.Constants; import com.lanyuanxiaoyao.service.common.entity.TableMeta; import com.lanyuanxiaoyao.service.common.utils.LogHelper; +import com.lanyuanxiaoyao.service.sync.utils.ExceptionUtils; import java.math.BigDecimal; import java.time.format.DateTimeFormatter; import java.util.ArrayList; @@ -115,7 +116,8 @@ public class TypeConverterV2 implements TypeConverter { return NULLABLE_STRING_SCHEMA; } } catch (Throwable throwable) { - throw new RuntimeException( + return ExceptionUtils.throwAndPrint( + logger, StrUtil.format("Convert type failure {} {} {} length: {} scala: {}", table, field, type, length, scala), throwable ); @@ -162,7 +164,8 @@ public class TypeConverterV2 implements TypeConverter { return value; } } catch (Throwable throwable) { - throw new RuntimeException( + return ExceptionUtils.throwAndPrint( + logger, StrUtil.format("Convert value failure {} {} {}", schema.toString(), name, value), throwable ); diff --git a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/utils/ExceptionUtils.java b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/utils/ExceptionUtils.java new file mode 100644 index 0000000..e1acd04 --- /dev/null +++ b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/utils/ExceptionUtils.java @@ -0,0 +1,26 @@ +package com.lanyuanxiaoyao.service.sync.utils; + +import org.slf4j.Logger; + +/** + * 处理异常抛出和打印 + * + * @author lanyuanxiaoyao + * @date 2024-10-22 + */ +public class ExceptionUtils { + public static T throwAndPrint(Logger logger, String content) { + logger.error(content); + throw new RuntimeException(content); + } + + public static T throwAndPrint(Logger logger, Throwable throwable) { + logger.error(throwable.getMessage(), throwable); + throw new RuntimeException(throwable); + } + + public static T throwAndPrint(Logger logger, String content, Throwable throwable) { + logger.error(content, throwable); + throw new RuntimeException(content, throwable); + } +} diff --git a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/utils/ZkUtils.java b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/utils/ZkUtils.java index c1059d8..50a3407 100644 --- a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/utils/ZkUtils.java +++ b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/utils/ZkUtils.java @@ -75,11 +75,9 @@ public class ZkUtils { .withMode(CreateMode.EPHEMERAL) .forPath(lockPath, runMeta.getBytes()); } catch (KeeperException.NodeExistsException e) { - logger.error("Lock exists for " + lockPath, e); - throw new RuntimeException(e); + ExceptionUtils.throwAndPrint(logger, "Lock exists for " + lockPath, e); } catch (Exception e) { - logger.error("Unknown error", e); - throw new RuntimeException(e); + ExceptionUtils.throwAndPrint(logger, "Unknown error", e); } } @@ -91,8 +89,7 @@ public class ZkUtils { } } } catch (Exception e) { - logger.error("Unknown error", e); - throw new RuntimeException(e); + ExceptionUtils.throwAndPrint(logger, "Unknown error", e); } } }