Skip to content

Commit

Permalink
handle SQLIntegrityConstraintViolationException, Column cannot be null (
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Apr 6, 2022
1 parent b28e882 commit b0cea3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,39 @@
*/
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
*/
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);
}

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);
}
}
14 changes: 3 additions & 11 deletions sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public boolean createBatch(List<? extends Object> 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
}

Expand Down Expand Up @@ -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);
}

}
Expand All @@ -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);
}
}

Expand Down

0 comments on commit b0cea3c

Please sign in to comment.