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

io.helidon.grpc.core.SafeStreamObserver Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2019, 2024 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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 io.helidon.grpc.core;

import java.lang.System.Logger.Level;

import io.grpc.Status;
import io.grpc.stub.StreamObserver;

/**
 * A {@link StreamObserver} that handles exceptions correctly.
 *
 * @param  the type of response expected
 */
public class SafeStreamObserver implements StreamObserver {

    /**
     * Create a {@link SafeStreamObserver} that wraps
     * another {@link StreamObserver}.
     *
     * @param streamObserver  the {@link StreamObserver} to wrap
     */
    private SafeStreamObserver(StreamObserver streamObserver) {
        delegate = streamObserver;
    }

    @Override
    public void onNext(T t) {
        if (done) {
            return;
        }

        if (t == null) {
            onError(Status.INVALID_ARGUMENT
                            .withDescription("onNext called with null. Null values are generally not allowed.")
                            .asRuntimeException());
        } else {
            try {
                delegate.onNext(t);
            } catch (Throwable thrown) {
                throwIfFatal(thrown);
                onError(thrown);
            }
        }
    }

    @Override
    public void onError(Throwable thrown) {
        try {
            if (done) {
                LOGGER.log(Level.ERROR, () -> "OnError called after StreamObserver was closed");
            } else {
                done = true;
                delegate.onError(checkNotNull(thrown));
            }
        } catch (Throwable t) {
            throwIfFatal(t);
            LOGGER.log(Level.ERROR, () -> "Caught exception handling onError", t);
        }
    }

    @Override
    public void onCompleted() {
        if (done) {
            LOGGER.log(Level.WARNING, "onComplete called after StreamObserver was closed");
        } else {
            try {
                done = true;
                delegate.onCompleted();
            } catch (Throwable thrown) {
                throwIfFatal(thrown);
                LOGGER.log(Level.ERROR, () -> "Caught exception handling onComplete", thrown);
            }
        }
    }

    /**
     * Obtain the wrapped {@link StreamObserver}.
     * @return  the wrapped {@link StreamObserver}
     */
    public StreamObserver delegate() {
        return delegate;
    }

    private Throwable checkNotNull(Throwable thrown) {
        if (thrown == null) {
            thrown = Status.INVALID_ARGUMENT
                    .withDescription("onError called with null Throwable. Null exceptions are generally not allowed.")
                    .asRuntimeException();
        }

        return thrown;
    }

    /**
     * Throws a particular {@code Throwable} only if it belongs to a set of "fatal" error varieties. These varieties are
     * as follows:
     * 
    *
  • {@code VirtualMachineError}
  • *
  • {@code ThreadDeath}
  • *
  • {@code LinkageError}
  • *
* * @param thrown the {@code Throwable} to test and perhaps throw */ private static void throwIfFatal(Throwable thrown) { if (thrown instanceof VirtualMachineError) { throw (VirtualMachineError) thrown; } else if (thrown instanceof ThreadDeath) { throw (ThreadDeath) thrown; } else if (thrown instanceof LinkageError) { throw (LinkageError) thrown; } } /** * Ensure that the specified {@link StreamObserver} is a safe observer. *

* If the specified observer is not an instance of {@link SafeStreamObserver} then wrap * it in a {@link SafeStreamObserver}. * * @param observer the {@link StreamObserver} to test * @param the response type expected by the observer * * @return a safe {@link StreamObserver} */ public static StreamObserver ensureSafeObserver(StreamObserver observer) { if (observer instanceof SafeStreamObserver) { return observer; } return new SafeStreamObserver<>(observer); } // ----- constants ------------------------------------------------------ /** * The {2link Logger} to use. */ private static final System.Logger LOGGER = System.getLogger(SafeStreamObserver.class.getName()); // ----- data members --------------------------------------------------- /** * The actual StreamObserver. */ private final StreamObserver delegate; /** * Indicates a terminal state. */ private boolean done; }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy