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

io.smallrye.mutiny.helpers.spies.UniOnFailureSpy Maven / Gradle / Ivy

package io.smallrye.mutiny.helpers.spies;

import java.util.function.Predicate;

import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.groups.UniOnFailure;
import io.smallrye.mutiny.subscription.UniSubscriber;

public class UniOnFailureSpy extends UniSpyBase {

    private Predicate predicate;
    private Class typeOfFailure;

    private volatile Throwable lastFailure;

    public Throwable lastFailure() {
        return lastFailure;
    }

    @Override
    public void reset() {
        super.reset();
        lastFailure = null;
    }

    UniOnFailureSpy(Uni upstream) {
        super(upstream);
    }

    UniOnFailureSpy(Uni upstream, Predicate predicate) {
        super(upstream);
        this.predicate = predicate;
    }

    UniOnFailureSpy(Uni upstream, Class typeOfFailure) {
        super(upstream);
        this.typeOfFailure = typeOfFailure;
    }

    @Override
    public void subscribe(UniSubscriber downstream) {
        UniOnFailure group;
        if (predicate != null) {
            group = upstream().onFailure(predicate);
        } else if (typeOfFailure != null) {
            group = upstream().onFailure(typeOfFailure);
        } else {
            group = upstream().onFailure();
        }
        group.invoke(failure -> {
            incrementInvocationCount();
            lastFailure = failure;
        }).subscribe().withSubscriber(downstream);
    }

    @Override
    public String toString() {
        return "UniOnFailureSpy{" +
                "lastFailure=" + lastFailure +
                "} " + super.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy