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

com.artipie.asto.s3.InternalExceptionHandle Maven / Gradle / Ivy

There is a newer version: v1.17.16
Show newest version
/*
 * The MIT License (MIT) Copyright (c) 2020-2023 artipie.com
 * https://github.com/artipie/asto/LICENSE.txt
 */
package com.artipie.asto.s3;

import com.artipie.asto.ArtipieIOException;
import com.artipie.asto.FailedCompletionStage;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * Translate an exception happened inside future.
 *
 * @param  Future result type.
 * @since 0.1
 */
final class InternalExceptionHandle implements BiFunction> {

    /**
     * Type of exception to handle.
     */
    private final Class from;

    /**
     * Converter to a new exception.
     */
    private final Function convert;

    /**
     * Ctor.
     *
     * @param from Internal type of exception.
     * @param convert Converter to a external type.
     */
    InternalExceptionHandle(
        final Class from,
        final Function convert
    ) {
        this.from = from;
        this.convert = convert;
    }

    @Override
    public CompletionStage apply(final T content, final Throwable throwable) {
        final CompletionStage result;
        if (throwable == null) {
            result = CompletableFuture.completedFuture(content);
        } else {
            if (
                throwable instanceof CompletionException
                    && this.from.isInstance(throwable.getCause())
            ) {
                result = new FailedCompletionStage<>(
                    this.convert.apply(throwable.getCause())
                );
            } else if (throwable instanceof CompletionException) {
                result = new FailedCompletionStage<>(new ArtipieIOException(throwable.getCause()));
            } else {
                result = new FailedCompletionStage<>(new ArtipieIOException(throwable));
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy