From b0cea3cb062bb5e8d133ac98898dcc5d28088ad8 Mon Sep 17 00:00:00 2001 From: simsevenx Date: Wed, 6 Apr 2022 18:47:16 +0800 Subject: [PATCH] handle SQLIntegrityConstraintViolationException, Column cannot be null (#47) --- .../xream/sqli/exception/ExceptionTranslator.java | 15 +++++++++++++-- .../io/xream/sqli/repository/dao/DaoImpl.java | 14 +++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/sqli-builder/src/main/java/io/xream/sqli/exception/ExceptionTranslator.java b/sqli-builder/src/main/java/io/xream/sqli/exception/ExceptionTranslator.java index 0268aedd..47c745e0 100644 --- a/sqli-builder/src/main/java/io/xream/sqli/exception/ExceptionTranslator.java +++ b/sqli-builder/src/main/java/io/xream/sqli/exception/ExceptionTranslator.java @@ -18,9 +18,12 @@ */ package io.xream.sqli.exception; +import io.xream.sqli.parser.Parser; import io.xream.sqli.util.SqliExceptionUtil; import org.slf4j.Logger; +import java.sql.SQLIntegrityConstraintViolationException; + /** * @author Sim */ @@ -28,8 +31,16 @@ public class ExceptionTranslator { private ExceptionTranslator(){} - public static SqliRuntimeException onRollback(Object obj, Exception e, Logger logger) { + public static SqliRuntimeException onRollback(Class clzz, Exception e, Logger logger) { Throwable t = SqliExceptionUtil.unwrapThrowable(e); + logger.error(SqliExceptionUtil.getMessage(t)); + if ( t instanceof SQLIntegrityConstraintViolationException){ + String msg = t.getMessage(); + if (msg.contains("cannot be null")) { + String prefix = (clzz == null ? "" : ("Table of "+ Parser.get(clzz).getTableName())); + throw new SqliRuntimeException(prefix+", " + msg); + } + } if (t instanceof RuntimeException) throw (RuntimeException)t; return new SqliRuntimeException(t); @@ -37,9 +48,9 @@ public static SqliRuntimeException onRollback(Object obj, Exception e, Logger l public static QueryException onQuery(Exception e, Logger logger) { Throwable t = SqliExceptionUtil.unwrapThrowable(e); + logger.error(SqliExceptionUtil.getMessage(t)); if (t instanceof RuntimeException) throw (RuntimeException)t; - logger.error(SqliExceptionUtil.getMessage(t)); return new QueryException(e); } } diff --git a/sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java b/sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java index 3be2e1b9..bbdd9e35 100644 --- a/sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java +++ b/sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java @@ -107,7 +107,7 @@ public boolean createBatch(List objList) { try { return this.jdbcHelper.createBatch(clz, sql, batchObjectValues, batchSize, this.dialect); } catch (Exception e) { - throw ExceptionTranslator.onRollback(obj, e, logger); + throw ExceptionTranslator.onRollback(clz, e, logger); }//1618978538016 } @@ -144,15 +144,7 @@ public boolean create(Object obj) { return this.jdbcHelper.create(isAutoIncreaseId,sql,valueList); } catch (Exception e) { - Throwable t = e.getCause(); - if ( t != null && - t instanceof SQLIntegrityConstraintViolationException){ - String msg = t.getMessage(); - if (msg.contains("cannot be null")) { - throw new SqliRuntimeException("Table of "+ Parser.get(clz).getTableName()+", " + msg); - } - } - throw ExceptionTranslator.onRollback(obj, e, logger); + throw ExceptionTranslator.onRollback(clz, e, logger); } } @@ -175,7 +167,7 @@ public boolean createOrReplace(Object obj) { return this.jdbcHelper.createOrReplace(sql, valueList); } catch (Exception e) { - throw ExceptionTranslator.onRollback(obj, e, logger); + throw ExceptionTranslator.onRollback(clz, e, logger); } }