All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.zero88.jooqx.JooqErrorConverter Maven / Gradle / Ivy

The newest version!
package io.zero88.jooqx;

import java.sql.SQLException;

import org.jooq.exception.DataAccessException;

import lombok.NonNull;

/**
 * Represents for SQL error maker that transforms SQL exception to {@code jOOQ} {@link DataAccessException}
 *
 * @param  Type of Throwable
 * @see DataAccessException
 * @since 1.0.0
 */
public interface JooqErrorConverter extends SQLErrorConverter {

    @NonNull DataAccessException transform(T t);

    @NonNull Class throwableType();

    @Override
    default RuntimeException handle(Throwable throwable) {
        if (throwableType().isInstance(throwable)) {
            return transform(throwableType().cast(throwable));
        }
        if (throwable instanceof SQLException) {
            return new JDBCErrorConverter().handle(throwable);
        }
        return SQLErrorConverter.super.handle(throwable);
    }

    class JDBCErrorConverter implements JooqErrorConverter {

        @Override
        public DataAccessException transform(SQLException t) {
            return new DataAccessException(t.getMessage(), t);
        }

        @Override
        public @NonNull Class throwableType() {
            return SQLException.class;
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy