1818
1919import com .google .common .base .MoreObjects ;
2020import com .google .common .collect .ImmutableMap ;
21- import com .google .gcloud .BaseServiceException ;
2221import com .google .gcloud .RetryHelper ;
2322import com .google .gcloud .RetryHelper .RetryHelperException ;
2423import com .google .gcloud .spi .DatastoreRpc .DatastoreRpcException ;
2726import java .util .HashMap ;
2827import java .util .Map ;
2928
30- public class DatastoreException extends BaseServiceException {
29+ public class DatastoreException extends RuntimeException {
3130
32- private static final long serialVersionUID = - 2336749234060754893L ;
33- private static final ImmutableMap <String , DatastoreError > REASON_TO_ERROR ;
34- private static final ImmutableMap <Integer , DatastoreError > HTTP_TO_ERROR ;
31+ private static final long serialVersionUID = 8170357898917041899L ;
32+ private static final ImmutableMap <String , Code > REASON_TO_CODE ;
33+ private static final ImmutableMap <Integer , Code > HTTP_TO_CODE ;
3534
36- private final DatastoreError error ;
35+ private final Code code ;
3736
3837 /**
39- * Represents Datastore errors .
38+ * An error code to represent the failure .
4039 *
4140 * @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud
4241 * Datastore error codes</a>
4342 */
44- public enum DatastoreError {
43+ public enum Code {
4544
4645 ABORTED (Reason .ABORTED ),
4746 DEADLINE_EXCEEDED (Reason .DEADLINE_EXCEEDED ),
@@ -58,25 +57,29 @@ public enum DatastoreError {
5857 private final String description ;
5958 private final int httpStatus ;
6059
61- DatastoreError (Reason reason ) {
60+ Code (Reason reason ) {
6261 this (reason .retryable (), reason .description (), reason .httpStatus ());
6362 }
6463
65- DatastoreError (boolean retryable , String description , int httpStatus ) {
64+ Code (boolean retryable , String description , int httpStatus ) {
6665 this .retryable = retryable ;
6766 this .description = description ;
6867 this .httpStatus = httpStatus ;
6968 }
7069
71- String description () {
70+ public String description () {
7271 return description ;
7372 }
7473
75- int httpStatus () {
74+ public int httpStatus () {
7675 return httpStatus ;
7776 }
7877
79- boolean retryable () {
78+ /**
79+ * Returns {@code true} if this exception is transient and the same request could be retried.
80+ * For any retry it is highly recommended to apply an exponential backoff.
81+ */
82+ public boolean retryable () {
8083 return retryable ;
8184 }
8285
@@ -86,31 +89,30 @@ DatastoreException translate(DatastoreRpcException exception, String message) {
8689 }
8790
8891 static {
89- ImmutableMap .Builder <String , DatastoreError > builder = ImmutableMap .builder ();
90- Map <Integer , DatastoreError > httpCodes = new HashMap <>();
91- for (DatastoreError error : DatastoreError .values ()) {
92- builder .put (error .name (), error );
93- httpCodes .put (error .httpStatus (), error );
92+ ImmutableMap .Builder <String , Code > builder = ImmutableMap .builder ();
93+ Map <Integer , Code > httpCodes = new HashMap <>();
94+ for (Code code : Code .values ()) {
95+ builder .put (code .name (), code );
96+ httpCodes .put (code .httpStatus (), code );
9497 }
95- REASON_TO_ERROR = builder .build ();
96- HTTP_TO_ERROR = ImmutableMap .copyOf (httpCodes );
98+ REASON_TO_CODE = builder .build ();
99+ HTTP_TO_CODE = ImmutableMap .copyOf (httpCodes );
97100 }
98101
99- public DatastoreException (DatastoreError error , String message , Exception cause ) {
100- super (error .httpStatus (), MoreObjects .firstNonNull (message , error .description ),
101- error .retryable (), cause );
102- this .error = error ;
102+ public DatastoreException (Code code , String message , Exception cause ) {
103+ super (MoreObjects .firstNonNull (message , code .description ), cause );
104+ this .code = code ;
103105 }
104106
105- public DatastoreException (DatastoreError error , String message ) {
106- this (error , message , null );
107+ public DatastoreException (Code code , String message ) {
108+ this (code , message , null );
107109 }
108110
109111 /**
110- * Returns the DatastoreError associated with this exception.
112+ * Returns the code associated with this exception.
111113 */
112- public DatastoreError datastoreError () {
113- return error ;
114+ public Code code () {
115+ return code ;
114116 }
115117
116118 static DatastoreException translateAndThrow (RetryHelperException ex ) {
@@ -120,36 +122,35 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
120122 if (ex instanceof RetryHelper .RetryInterruptedException ) {
121123 RetryHelper .RetryInterruptedException .propagate ();
122124 }
123- throw new DatastoreException (DatastoreError .UNKNOWN , ex .getMessage (), ex );
125+ throw new DatastoreException (Code .UNKNOWN , ex .getMessage (), ex );
124126 }
125127
126128 /**
127- * Translate DatastoreRpcExceptions to DatastoreExceptions based on their
129+ * Translate DatastoreException to DatastoreException based on their
128130 * HTTP error codes. This method will always throw a new DatastoreException.
129131 *
130132 * @throws DatastoreException every time
131133 */
132134 static DatastoreException translateAndThrow (DatastoreRpcException exception ) {
133135 String message = exception .getMessage ();
134- DatastoreError error = REASON_TO_ERROR .get (exception .reason ());
135- if (error == null ) {
136- error = MoreObjects .firstNonNull (
137- HTTP_TO_ERROR .get (exception .httpStatus ()), DatastoreError .UNKNOWN );
136+ Code code = REASON_TO_CODE .get (exception .reason ());
137+ if (code == null ) {
138+ code = MoreObjects .firstNonNull (HTTP_TO_CODE .get (exception .httpStatus ()), Code .UNKNOWN );
138139 }
139- throw error .translate (exception , message );
140+ throw code .translate (exception , message );
140141 }
141142
142143 /**
143- * Throw a DatastoreException with {@code FAILED_PRECONDITION} error and the {@code message}
144+ * Throw a DatastoreException with {@code FAILED_PRECONDITION} code and the {@code message}
144145 * in a nested exception.
145146 *
146147 * @throws DatastoreException every time
147148 */
148149 static DatastoreException throwInvalidRequest (String massage , Object ... params ) {
149- throw new DatastoreException (DatastoreError .FAILED_PRECONDITION , String .format (massage , params ));
150+ throw new DatastoreException (Code .FAILED_PRECONDITION , String .format (massage , params ));
150151 }
151152
152153 static DatastoreException propagateUserException (Exception ex ) {
153- throw new DatastoreException (DatastoreError .UNKNOWN , ex .getMessage (), ex );
154+ throw new DatastoreException (Code .UNKNOWN , ex .getMessage (), ex );
154155 }
155156}
0 commit comments