blob: 7698533054c5e2ede2c2e42a49609efad23a51a1 [file] [log] [blame]
ejona07d3f6a2014-05-14 11:26:57 -07001package com.google.net.stubby;
2
3import com.google.common.base.Preconditions;
4import com.google.net.stubby.transport.Transport;
5
6import javax.annotation.Nullable;
7import javax.annotation.concurrent.Immutable;
8
9/**
10 * Defines the status of an operation using the canonical error space.
11 */
12@Immutable
13public class Status {
14
15 public static final Status OK = new Status(Transport.Code.OK);
16
17 private final Transport.Code code;
18 private final String description;
19 private final Throwable cause;
20
21 public Status(Transport.Code code) {
22 this(code, null, null);
23 }
24
25 public Status(Transport.Code code, @Nullable String description) {
26 this(code, description, null);
27 }
28
29 public Status(Transport.Code code, @Nullable Throwable cause) {
30 this(code, null, cause);
31 }
32
33 public Status(Transport.Code code, @Nullable String description, @Nullable Throwable cause) {
34 this.code = Preconditions.checkNotNull(code);
35 this.description = description;
36 this.cause = cause;
37 }
38
39 public Transport.Code getCode() {
40 return code;
41 }
42
43 @Nullable
44 public String getDescription() {
45 return description;
46 }
47
48 @Nullable
49 public Throwable getCause() {
50 return cause;
51 }
52
53 /**
54 * Override this status with another if allowed.
55 */
56 public Status overrideWith(Status newStatus) {
57 if (this.getCode() == Transport.Code.OK || newStatus.code == Transport.Code.OK) {
58 return this;
59 } else {
60 return newStatus;
61 }
62 }
63
64 public RuntimeException asRuntimeException() {
65 return new OperationRuntimeException(this);
66 }
67
68 public Exception asException() {
69 return new OperationException(this);
70 }
71
72 /**
73 * Exception thrown by implementations while managing an operation.
74 */
75 public static class OperationException extends Exception {
76
77 private final Status status;
78
79 public OperationException(Status status) {
80 super(status.getDescription(), status.getCause());
81 this.status = status;
82 }
83
84 public Status getStatus() {
85 return status;
86 }
87 }
88
89 /**
90 * Runtime exception thrown by implementations while managing an operation.
91 */
92 public static class OperationRuntimeException extends RuntimeException {
93
94 private final Status status;
95
96 public OperationRuntimeException(Status status) {
97 super(status.getDescription(), status.getCause());
98 this.status = status;
99 }
100
101 public Status getStatus() {
102 return status;
103 }
104 }
zhangkun347a22d2014-05-21 16:44:20 -0700105
106 @Override
107 public String toString() {
108 StringBuilder builder = new StringBuilder();
109 builder.append("[").append(code);
110 if (description != null) {
111 builder.append(";").append(description);
112 }
113 if (cause != null) {
114 builder.append(";").append(cause);
115 }
116 builder.append("]");
117 return builder.toString();
118 }
ejona07d3f6a2014-05-14 11:26:57 -0700119}