io.zero88.jooqx.SQLErrorConverter Maven / Gradle / Ivy
The newest version!
package io.zero88.jooqx;
import java.util.function.Function;
import lombok.NonNull;
/**
* Represents for a maker that unifies SQL error to a general SQL error when executing SQL command on across among
* Database SPI
*
* @since 1.0.0
*/
public interface SQLErrorConverter {
SQLErrorConverter DEFAULT = new SQLErrorConverter() {};
/**
* Handle throwable to runtime exception
*
* @param throwable throwable
* @return runtime exception that wraps given exception if the given is not runtime exception
*/
default RuntimeException handle(Throwable throwable) {
return throwable instanceof RuntimeException ? (RuntimeException) throwable : new RuntimeException(throwable);
}
/**
* Handle and throw error
*
* @param throwable throwable
* @param any type
* @return nothing because it will throw error
* @see #handle(Throwable)
*/
default X reThrowError(@NonNull Throwable throwable) {
throw this.handle(throwable);
}
/**
* Create new SQL error converter to easy integrate with current application exception
*
* @param andThen function that transform output of current {@link #handle(Throwable)} to an expectation
* @param Type of runtime exception
* @return new SQL error converter
*/
default SQLErrorConverter andThen(@NonNull Function andThen) {
return new SQLErrorConverter() {
@Override
public RuntimeException handle(Throwable throwable) {
return andThen.apply(SQLErrorConverter.this.handle(throwable));
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy