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

org.zodiac.fastorm.rdb.utils.ExceptionUtils Maven / Gradle / Ivy

The newest version!
package org.zodiac.fastorm.rdb.utils;

import org.reactivestreams.Publisher;
import org.zodiac.fastorm.core.meta.FeatureSupportedMetadata;
import org.zodiac.fastorm.rdb.operator.ExceptionTranslation;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.function.Function;
import java.util.function.Supplier;

public abstract class ExceptionUtils {

    private ExceptionUtils() {
        super();
    }

    public static Throwable translation(FeatureSupportedMetadata metadata, Throwable e) {
        return metadata.findFeature(ExceptionTranslation.ID)
                .map(trans -> trans.translate(e))
                .orElse(e);
    }

    public static  T translation(Supplier supplier, FeatureSupportedMetadata metadata) {
        T result = null;
        try {
            result =  supplier.get();
        } catch (Throwable r) {
            try {
                throw translation(metadata, r);
            }catch (Throwable e) {
                ExceptionUtil.chuck(e);
            }
        }
        return result;
    }

    @SuppressWarnings("all")
    public static > Function translation(FeatureSupportedMetadata metadata) {
        Function result = null;
        try {
            result = publisher -> {
                if (publisher instanceof Mono) {
                    return (P)((Mono) publisher).onErrorMap(err -> translation(metadata, err));
                }
                if (publisher instanceof Flux) {
                    return (P)((Flux) publisher).onErrorMap(err -> translation(metadata, err));
                }
                return publisher;
            };
        } catch (Exception e) {
            ExceptionUtil.chuck(e);
        }
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy