Returns the Status of a cancelled context or null if the context is not cancelled.
(Context context)
| 125 | * is not cancelled. |
| 126 | */ |
| 127 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1975") |
| 128 | public static Status statusFromCancelled(Context context) { |
| 129 | Preconditions.checkNotNull(context, "context must not be null"); |
| 130 | if (!context.isCancelled()) { |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | Throwable cancellationCause = context.cancellationCause(); |
| 135 | if (cancellationCause == null) { |
| 136 | return Status.CANCELLED.withDescription("io.grpc.Context was cancelled without error"); |
| 137 | } |
| 138 | if (cancellationCause instanceof TimeoutException) { |
| 139 | return Status.DEADLINE_EXCEEDED |
| 140 | .withDescription(cancellationCause.getMessage()) |
| 141 | .withCause(cancellationCause); |
| 142 | } |
| 143 | Status status = Status.fromThrowable(cancellationCause); |
| 144 | if (Status.Code.UNKNOWN.equals(status.getCode()) |
| 145 | && status.getCause() == cancellationCause) { |
| 146 | // If fromThrowable could not determine a status, then |
| 147 | // just return CANCELLED. |
| 148 | return Status.CANCELLED.withDescription("Context cancelled").withCause(cancellationCause); |
| 149 | } |
| 150 | return status.withCause(cancellationCause); |
| 151 | } |
| 152 | } |