/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.spine3.base;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import io.grpc.Metadata;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Utility class for working with {@link Error}s.
 *
 * @author Alexander Yevsyukov
 */
public class Errors {

    private Errors() {
        // Prevent instantiation of this utility class.
    }

    /** Creates new instance of {@link Error} by the passed exception. */
    public static Error fromException(Exception exception) {
        checkNotNull(exception);
        final String message = exception.getMessage();
        final Error result = Error.newBuilder()
                                  .setType(exception.getClass()
                                                    .getName())
                                  .setMessage(message)
                                  .setStacktrace(Throwables.getStackTraceAsString(exception))
                                  .build();
        return result;
    }

    /** Creates new instance of {@link Error} by the passed {@code Throwable}. */
    public static Error fromThrowable(Throwable throwable) {
        checkNotNull(throwable);
        final String message = Strings.nullToEmpty(throwable.getMessage());
        final Error result = Error.newBuilder()
                                  .setType(throwable.getClass()
                                                    .getName())
                                  .setMessage(message)
                                  .setStacktrace(Throwables.getStackTraceAsString(throwable))
                                  .build();
        return result;
    }

    /**
     * Extracts an {@link Error} either from the {@link StatusRuntimeException}
     * or from the {@linkplain io.grpc.StatusException StatusException} metadata using
     * {@linkplain MetadataConverter#toError(Metadata) MetadataConverter}.
     *
     * <p>If any other exception was passed, {@code Optional.absent()} is returned.
     *
     * <p>The typical place where this method must be used is
     * {@link io.grpc.stub.StreamObserver#onError(Throwable) StreamObserver.onError(Throwable)}.
     *
     * @param throwable the {@code Throwable} to extract an {@link Error}
     * @return the extracted error or {@code Optional.absent()} if the extraction failed
     */
    @SuppressWarnings("ChainOfInstanceofChecks") // Only way to check an exact throwable type.
    public static Optional<Error> fromStreamError(Throwable throwable) {
        checkNotNull(throwable);
        if (throwable instanceof StatusRuntimeException) {
            final Metadata metadata = ((StatusRuntimeException) throwable).getTrailers();
            return MetadataConverter.toError(metadata);
        }
        if (throwable instanceof StatusException) {
            final Metadata metadata = ((StatusException) throwable).getTrailers();
            return MetadataConverter.toError(metadata);
        }

        return Optional.absent();
    }
}

@@ -2823,142 +2823,284 @@
 ts a
-n
  %7B@link
+plain
  Error
-%7D either from the %7B@link StatusRuntimeException%7D%0A     * or from the %7B@linkplain io.grpc.StatusException StatusException%7D
+ system error%7D from the%0A     * %7B@linkplain io.grpc.stub.StreamObserver#onError(Throwable) Throwable%7D,%0A     * received on a client-side as a result of a failed gRPC call to server-side routines.%0A     *%0A     * %3Cp%3EThe %7B@code Error%7D is extracted from the trailer
  met
@@ -3109,21 +3109,18 @@
 ata 
-using
+of
 %0A     * 
 %7B@li
@@ -3119,74 +3119,77 @@
   * 
-%7B@linkplain MetadataConverter#toError(Metadata) MetadataConverter%7D
+either %7B@link StatusRuntimeException%7D or %7B@link StatusException%7D only
 .%0A  
@@ -3226,18 +3226,17 @@
 ception 
-wa
+i
 s passed
@@ -3287,174 +3287,8 @@
   *%0A
-     * %3Cp%3EThe typical place where this method must be used is%0A     * %7B@link io.grpc.stub.StreamObserver#onError(Throwable) StreamObserver.onError(Throwable)%7D.%0A     *%0A
     

Clarify `Errors.fromStreamError` Javadoc