@Test public void statusFromCancelled_returnCancelledIfCauseIsNull() { Context.CancellableContext cancellableContext = Context.current().withCancellation(); cancellableContext.cancel(null); assertTrue(cancellableContext.isCancelled()); Status status = statusFromCancelled(cancellableContext); assertNotNull(status); assertEquals(Status.Code.CANCELLED, status.getCode()); }
@ExperimentalApi("https: public static Status statusFromCancelled(Context context) { Preconditions.checkNotNull(context, "context must not be null"); if (!context.isCancelled()) { return null; } Throwable cancellationCause = context.cancellationCause(); if (cancellationCause == null) { return Status.CANCELLED; } if (cancellationCause instanceof TimeoutException) { return Status.DEADLINE_EXCEEDED .withDescription(cancellationCause.getMessage()) .withCause(cancellationCause); } Status status = Status.fromThrowable(cancellationCause); if (Status.Code.UNKNOWN.equals(status.getCode()) && status.getCause() == cancellationCause) { return Status.CANCELLED.withCause(cancellationCause); } return status.withCause(cancellationCause); }
Contexts { @ExperimentalApi("https: public static Status statusFromCancelled(Context context) { Preconditions.checkNotNull(context, "context must not be null"); if (!context.isCancelled()) { return null; } Throwable cancellationCause = context.cancellationCause(); if (cancellationCause == null) { return Status.CANCELLED; } if (cancellationCause instanceof TimeoutException) { return Status.DEADLINE_EXCEEDED .withDescription(cancellationCause.getMessage()) .withCause(cancellationCause); } Status status = Status.fromThrowable(cancellationCause); if (Status.Code.UNKNOWN.equals(status.getCode()) && status.getCause() == cancellationCause) { return Status.CANCELLED.withCause(cancellationCause); } return status.withCause(cancellationCause); } }
Contexts { @ExperimentalApi("https: public static Status statusFromCancelled(Context context) { Preconditions.checkNotNull(context, "context must not be null"); if (!context.isCancelled()) { return null; } Throwable cancellationCause = context.cancellationCause(); if (cancellationCause == null) { return Status.CANCELLED; } if (cancellationCause instanceof TimeoutException) { return Status.DEADLINE_EXCEEDED .withDescription(cancellationCause.getMessage()) .withCause(cancellationCause); } Status status = Status.fromThrowable(cancellationCause); if (Status.Code.UNKNOWN.equals(status.getCode()) && status.getCause() == cancellationCause) { return Status.CANCELLED.withCause(cancellationCause); } return status.withCause(cancellationCause); } private  Contexts(); }
Contexts { @ExperimentalApi("https: public static Status statusFromCancelled(Context context) { Preconditions.checkNotNull(context, "context must not be null"); if (!context.isCancelled()) { return null; } Throwable cancellationCause = context.cancellationCause(); if (cancellationCause == null) { return Status.CANCELLED; } if (cancellationCause instanceof TimeoutException) { return Status.DEADLINE_EXCEEDED .withDescription(cancellationCause.getMessage()) .withCause(cancellationCause); } Status status = Status.fromThrowable(cancellationCause); if (Status.Code.UNKNOWN.equals(status.getCode()) && status.getCause() == cancellationCause) { return Status.CANCELLED.withCause(cancellationCause); } return status.withCause(cancellationCause); } private  Contexts(); static ServerCall.Listener<ReqT> interceptCall(
        Context context,
        MethodDescriptor<ReqT, RespT> method,
        ServerCall<RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next); @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1705") static Status statusFromCancelled(Context context); }
Contexts { @ExperimentalApi("https: public static Status statusFromCancelled(Context context) { Preconditions.checkNotNull(context, "context must not be null"); if (!context.isCancelled()) { return null; } Throwable cancellationCause = context.cancellationCause(); if (cancellationCause == null) { return Status.CANCELLED; } if (cancellationCause instanceof TimeoutException) { return Status.DEADLINE_EXCEEDED .withDescription(cancellationCause.getMessage()) .withCause(cancellationCause); } Status status = Status.fromThrowable(cancellationCause); if (Status.Code.UNKNOWN.equals(status.getCode()) && status.getCause() == cancellationCause) { return Status.CANCELLED.withCause(cancellationCause); } return status.withCause(cancellationCause); } private  Contexts(); static ServerCall.Listener<ReqT> interceptCall(
        Context context,
        MethodDescriptor<ReqT, RespT> method,
        ServerCall<RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next); @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1705") static Status statusFromCancelled(Context context);  }