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

org.zalando.riptide.faults.DefaultFaultClassifier Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package org.zalando.riptide.faults;

import lombok.AllArgsConstructor;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.function.Predicate;
import java.util.stream.Stream;

@AllArgsConstructor
public final class DefaultFaultClassifier implements FaultClassifier {

    private final ClassificationStrategy strategy;
    private final Predicate included;
    private final Predicate excluded;

    public DefaultFaultClassifier() {
        this(new CausalChainClassificationStrategy());
    }

    public DefaultFaultClassifier(final ClassificationStrategy strategy) {
        this(strategy,
                IOException.class::isInstance,
                merge(
                        UnknownHostException.class::isInstance,
                        throwable -> throwable instanceof SSLException
                                && !(throwable instanceof SSLHandshakeException
                                        && "Remote host closed connection during handshake".equals(throwable.getMessage())),
                        MalformedURLException.class::isInstance
                ));
    }

    @SafeVarargs
    private static Predicate merge(final Predicate predicate,
            final Predicate... predicates) {
        return Stream.of(predicates).reduce(predicate, Predicate::or);
    }

    public DefaultFaultClassifier include(final Predicate predicate) {
        return new DefaultFaultClassifier(strategy, included.or(predicate), excluded);
    }

    public DefaultFaultClassifier exclude(final Predicate predicate) {
        return new DefaultFaultClassifier(strategy, included, excluded.or(predicate));
    }

    @Override
    public Throwable classify(final Throwable throwable) {
        if (throwable instanceof TransientFaultException) {
            return throwable;
        }

        if (!strategy.test(throwable, excluded) && strategy.test(throwable, included)) {
            return new TransientFaultException(throwable);
        }

        return throwable;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy