ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 1 | package com.google.net.stubby; |
| 2 | |
| 3 | import com.google.common.base.Preconditions; |
lryan | 2ce8446 | 2014-06-02 14:43:36 -0700 | [diff] [blame] | 4 | import com.google.common.base.Throwables; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 5 | import com.google.net.stubby.transport.Transport; |
| 6 | |
| 7 | import javax.annotation.Nullable; |
| 8 | import javax.annotation.concurrent.Immutable; |
| 9 | |
| 10 | /** |
| 11 | * Defines the status of an operation using the canonical error space. |
| 12 | */ |
| 13 | @Immutable |
| 14 | public class Status { |
| 15 | |
| 16 | public static final Status OK = new Status(Transport.Code.OK); |
nathanmittler | cc7cdb1 | 2014-07-11 12:00:32 -0700 | [diff] [blame] | 17 | public static final Status CANCELLED = new Status(Transport.Code.CANCELLED); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 18 | |
lryan | 2ce8446 | 2014-06-02 14:43:36 -0700 | [diff] [blame] | 19 | public static Status fromThrowable(Throwable t) { |
| 20 | for (Throwable cause : Throwables.getCausalChain(t)) { |
| 21 | if (cause instanceof OperationException) { |
| 22 | return ((Status.OperationException) cause).getStatus(); |
| 23 | } else if (cause instanceof OperationRuntimeException) { |
| 24 | return ((Status.OperationRuntimeException) cause).getStatus(); |
| 25 | } |
| 26 | } |
| 27 | // Couldn't find a cause with a Status |
| 28 | return new Status(Transport.Code.INTERNAL, t); |
| 29 | } |
| 30 | |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 31 | private final Transport.Code code; |
| 32 | private final String description; |
| 33 | private final Throwable cause; |
| 34 | |
| 35 | public Status(Transport.Code code) { |
| 36 | this(code, null, null); |
| 37 | } |
| 38 | |
| 39 | public Status(Transport.Code code, @Nullable String description) { |
| 40 | this(code, description, null); |
| 41 | } |
| 42 | |
| 43 | public Status(Transport.Code code, @Nullable Throwable cause) { |
| 44 | this(code, null, cause); |
| 45 | } |
| 46 | |
| 47 | public Status(Transport.Code code, @Nullable String description, @Nullable Throwable cause) { |
| 48 | this.code = Preconditions.checkNotNull(code); |
| 49 | this.description = description; |
| 50 | this.cause = cause; |
| 51 | } |
| 52 | |
| 53 | public Transport.Code getCode() { |
| 54 | return code; |
| 55 | } |
| 56 | |
| 57 | @Nullable |
| 58 | public String getDescription() { |
| 59 | return description; |
| 60 | } |
| 61 | |
| 62 | @Nullable |
| 63 | public Throwable getCause() { |
| 64 | return cause; |
| 65 | } |
| 66 | |
ejona | 35cabd0 | 2014-06-11 14:46:25 -0700 | [diff] [blame] | 67 | public boolean isOk() { |
| 68 | return OK.getCode() == getCode(); |
| 69 | } |
| 70 | |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Override this status with another if allowed. |
| 73 | */ |
| 74 | public Status overrideWith(Status newStatus) { |
| 75 | if (this.getCode() == Transport.Code.OK || newStatus.code == Transport.Code.OK) { |
| 76 | return this; |
| 77 | } else { |
| 78 | return newStatus; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public RuntimeException asRuntimeException() { |
| 83 | return new OperationRuntimeException(this); |
| 84 | } |
| 85 | |
| 86 | public Exception asException() { |
| 87 | return new OperationException(this); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Exception thrown by implementations while managing an operation. |
| 92 | */ |
| 93 | public static class OperationException extends Exception { |
| 94 | |
| 95 | private final Status status; |
| 96 | |
| 97 | public OperationException(Status status) { |
zhangkun | e1ae25c | 2014-08-08 22:39:39 -0700 | [diff] [blame] | 98 | super(status.getCode() + ": " + status.getDescription(), status.getCause()); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 99 | this.status = status; |
| 100 | } |
| 101 | |
| 102 | public Status getStatus() { |
| 103 | return status; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Runtime exception thrown by implementations while managing an operation. |
| 109 | */ |
| 110 | public static class OperationRuntimeException extends RuntimeException { |
| 111 | |
| 112 | private final Status status; |
| 113 | |
| 114 | public OperationRuntimeException(Status status) { |
zhangkun | e1ae25c | 2014-08-08 22:39:39 -0700 | [diff] [blame] | 115 | super(status.getCode() + ": " + status.getDescription(), status.getCause()); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 116 | this.status = status; |
| 117 | } |
| 118 | |
| 119 | public Status getStatus() { |
| 120 | return status; |
| 121 | } |
| 122 | } |
zhangkun | 347a22d | 2014-05-21 16:44:20 -0700 | [diff] [blame] | 123 | |
| 124 | @Override |
| 125 | public String toString() { |
| 126 | StringBuilder builder = new StringBuilder(); |
| 127 | builder.append("[").append(code); |
| 128 | if (description != null) { |
| 129 | builder.append(";").append(description); |
| 130 | } |
| 131 | if (cause != null) { |
| 132 | builder.append(";").append(cause); |
| 133 | } |
| 134 | builder.append("]"); |
| 135 | return builder.toString(); |
| 136 | } |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 137 | } |