1
mirror of https://github.com/CarmJos/EasySQL.git synced 2024-09-19 21:35:47 +00:00

[v0.2.9] [A] 添加自定义默认异常处理器的方法。

This commit is contained in:
Carm Jos 2022-01-24 15:53:07 +08:00
parent 360b416525
commit a8a475fc7a
4 changed files with 51 additions and 19 deletions

View File

@ -3,10 +3,12 @@ package cc.carm.lib.easysql.api;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import cc.carm.lib.easysql.api.function.SQLFunction;
import cc.carm.lib.easysql.api.function.SQLHandler;
import cc.carm.lib.easysql.api.function.impl.DefaultSQLExceptionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
/**
@ -147,7 +149,7 @@ public interface SQLAction<T> {
@Nullable SQLExceptionHandler failure);
default void handleException(@Nullable SQLExceptionHandler handler, SQLException exception) {
if (handler == null) handler = getExceptionHandler();
if (handler == null) handler = defaultExceptionHandler();
handler.accept(exception, this);
}
@ -155,22 +157,10 @@ public interface SQLAction<T> {
* @return 默认的异常处理器
*/
default SQLExceptionHandler defaultExceptionHandler() {
return (exception, action) -> {
getManager().getLogger().severe("Error when execute [" + action.getSQLContent() + "]");
getManager().getLogger().severe(exception.getLocalizedMessage());
exception.printStackTrace();
};
return Optional.ofNullable(DefaultSQLExceptionHandler.getCustomHandler())
.orElse(new DefaultSQLExceptionHandler(getManager()));
}
/**
* 得到通用的异常处理器
* <br> 若未使用 {@link #setExceptionHandler(SQLExceptionHandler)} 方法
* <br> 则会返回 {@link #defaultExceptionHandler()}
*
* @return 通用异常处理器
*/
@NotNull SQLExceptionHandler getExceptionHandler();
/**
* 设定通用的异常处理器
* <br> 在使用 {@link #execute(SQLExceptionHandler)} 等相关方法时若传入的处理器为null则会采用此处理器
@ -178,6 +168,8 @@ public interface SQLAction<T> {
*
* @param handler 异常处理器
*/
void setExceptionHandler(@Nullable SQLExceptionHandler handler);
default void setExceptionHandler(@Nullable SQLExceptionHandler handler) {
DefaultSQLExceptionHandler.setCustomHandler(handler);
}
}

View File

@ -0,0 +1,40 @@
package cc.carm.lib.easysql.api.function.impl;
import cc.carm.lib.easysql.api.SQLAction;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
private static @Nullable SQLExceptionHandler customDefaultHandler = null;
public static void setCustomHandler(@Nullable SQLExceptionHandler handler) {
DefaultSQLExceptionHandler.customDefaultHandler = handler;
}
public static @Nullable SQLExceptionHandler getCustomHandler() {
return customDefaultHandler;
}
private final SQLManager sqlManager;
public DefaultSQLExceptionHandler(SQLManager manager) {
this.sqlManager = manager;
}
protected SQLManager getManager() {
return sqlManager;
}
@Override
public void accept(SQLException exception, SQLAction<?> sqlAction) {
getManager().getLogger().severe("Error when execute [" + sqlAction.getSQLContent() + "]");
getManager().getLogger().severe(exception.getLocalizedMessage());
exception.printStackTrace();
}
}

View File

@ -8,8 +8,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiConsumer;
public abstract class AbstractSQLAction<T> implements SQLAction<T> {
@ -20,7 +20,7 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
protected @NotNull String sqlContent;
protected @Nullable BiConsumer<SQLException, SQLAction<T>> exceptionHandler = null;
protected static @Nullable SQLExceptionHandler exceptionHandler = null;
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) {
this(manager, sql, System.currentTimeMillis());

View File

@ -70,7 +70,7 @@ public class SQLQueryImpl implements SQLQuery {
);
getManager().getActiveQuery().remove(getAction().getActionUUID());
} catch (SQLException e) {
getAction().handleException(getAction().getExceptionHandler(), e);
getAction().handleException(getAction().defaultExceptionHandler(), e);
}
this.queryAction = null;
}