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

software.amazon.kinesis.retrieval.AWSExceptionManager Maven / Gradle / Ivy

Go to download

The Amazon Kinesis Client Library for Java enables Java developers to easily consume and process data from Amazon Kinesis.

There is a newer version: 3.0.1
Show newest version
/*
 * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Amazon Software License (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 * http://aws.amazon.com/asl/
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package software.amazon.kinesis.retrieval;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import lombok.NonNull;
import lombok.Setter;
import lombok.experimental.Accessors;
import software.amazon.kinesis.annotations.KinesisClientInternalApi;

/**
 *
 */
@KinesisClientInternalApi
public class AWSExceptionManager {
    private final Map, Function> map = new HashMap<>();

    @Setter
    @Accessors(fluent = true)
    private Function defaultFunction = RuntimeException::new;

    public  void add(@NonNull final Class clazz,
            @NonNull final Function function) {
        map.put(clazz, function);
    }

    @SuppressWarnings("unchecked")
    private Function handleFor(@NonNull final Throwable t) {
        Class clazz = t.getClass();
        Optional> toApply = Optional.ofNullable(map.get(clazz));
        while (!toApply.isPresent() && clazz.getSuperclass() != null) {
            clazz = (Class) clazz.getSuperclass();
            toApply = Optional.ofNullable(map.get(clazz));
        }

        return toApply.orElse(defaultFunction);
    }

    @SuppressWarnings("unchecked")
    public RuntimeException apply(Throwable t) {
        //
        // We know this is safe as the handler guarantees that the function we get will be able to accept the actual
        // type of the throwable.  handlerFor walks up the inheritance chain so we can't get a function more specific
        // than the actual type of the throwable only.
        //
        Function f =
                (Function) handleFor(t);
        return f.apply(t);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy