remove the dependency on Guava's Throwables.getCausalChain #1663 (#1749)

resolves #1663
This commit is contained in:
ZHANG Dapeng 2016-04-29 19:43:21 -07:00
parent 51548bc09d
commit dda4ad7441
1 changed files with 9 additions and 4 deletions

View File

@ -31,10 +31,10 @@
package io.grpc; package io.grpc;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import io.grpc.Metadata.AsciiMarshaller; import io.grpc.Metadata.AsciiMarshaller;
@ -48,6 +48,7 @@ import java.util.TreeMap;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
/** /**
* Defines the status of an operation by providing a standard {@link Code} in conjunction with an * Defines the status of an operation by providing a standard {@link Code} in conjunction with an
* optional descriptive message. Instances of {@code Status} are created by starting with the * optional descriptive message. Instances of {@code Status} are created by starting with the
@ -392,16 +393,20 @@ public final class Status {
/** /**
* Extract an error {@link Status} from the causal chain of a {@link Throwable}. * Extract an error {@link Status} from the causal chain of a {@link Throwable}.
* If no status can be found, a status is created with {@link Code#UNKNOWN} as its code and
* {@code t} as its cause.
* *
* @return non-{@code null} status * @return non-{@code null} status
*/ */
public static Status fromThrowable(Throwable t) { public static Status fromThrowable(Throwable t) {
for (Throwable cause : Throwables.getCausalChain(t)) { Throwable cause = checkNotNull(t);
while (cause != null) {
if (cause instanceof StatusException) { if (cause instanceof StatusException) {
return ((StatusException) cause).getStatus(); return ((StatusException) cause).getStatus();
} else if (cause instanceof StatusRuntimeException) { } else if (cause instanceof StatusRuntimeException) {
return ((StatusRuntimeException) cause).getStatus(); return ((StatusRuntimeException) cause).getStatus();
} }
cause = cause.getCause();
} }
// Couldn't find a cause with a Status // Couldn't find a cause with a Status
return UNKNOWN.withCause(t); return UNKNOWN.withCause(t);
@ -424,7 +429,7 @@ public final class Status {
} }
private Status(Code code, @Nullable String description, @Nullable Throwable cause) { private Status(Code code, @Nullable String description, @Nullable Throwable cause) {
this.code = Preconditions.checkNotNull(code); this.code = checkNotNull(code);
this.description = description; this.description = description;
this.cause = cause; this.cause = cause;
} }