1818
1919import com .google .common .base .MoreObjects ;
2020import com .google .common .collect .ImmutableMap ;
21+ import com .google .gcloud .BaseServiceException ;
2122import com .google .gcloud .RetryHelper ;
2223import com .google .gcloud .RetryHelper .RetryHelperException ;
2324import com .google .gcloud .spi .DatastoreRpc .DatastoreRpcException ;
2627import java .util .HashMap ;
2728import java .util .Map ;
2829
29- public class DatastoreException extends RuntimeException {
30+ public class DatastoreException extends BaseServiceException {
3031
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 ;
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 ;
3435
35- private final Code code ;
36+ private final DatastoreError error ;
3637
3738 /**
38- * An error code to represent the failure .
39+ * Represents Datastore errors .
3940 *
4041 * @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud
4142 * Datastore error codes</a>
4243 */
43- public enum Code {
44+ public enum DatastoreError {
4445
4546 ABORTED (Reason .ABORTED ),
4647 DEADLINE_EXCEEDED (Reason .DEADLINE_EXCEEDED ),
@@ -57,29 +58,25 @@ public enum Code {
5758 private final String description ;
5859 private final int httpStatus ;
5960
60- Code (Reason reason ) {
61+ DatastoreError (Reason reason ) {
6162 this (reason .retryable (), reason .description (), reason .httpStatus ());
6263 }
6364
64- Code (boolean retryable , String description , int httpStatus ) {
65+ DatastoreError (boolean retryable , String description , int httpStatus ) {
6566 this .retryable = retryable ;
6667 this .description = description ;
6768 this .httpStatus = httpStatus ;
6869 }
6970
70- public String description () {
71+ String description () {
7172 return description ;
7273 }
7374
74- public int httpStatus () {
75+ int httpStatus () {
7576 return httpStatus ;
7677 }
7778
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 () {
79+ boolean retryable () {
8380 return retryable ;
8481 }
8582
@@ -89,30 +86,31 @@ DatastoreException translate(DatastoreRpcException exception, String message) {
8986 }
9087
9188 static {
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 );
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 );
9794 }
98- REASON_TO_CODE = builder .build ();
99- HTTP_TO_CODE = ImmutableMap .copyOf (httpCodes );
95+ REASON_TO_ERROR = builder .build ();
96+ HTTP_TO_ERROR = ImmutableMap .copyOf (httpCodes );
10097 }
10198
102- public DatastoreException (Code code , String message , Exception cause ) {
103- super (MoreObjects .firstNonNull (message , code .description ), cause );
104- this .code = code ;
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 ;
105103 }
106104
107- public DatastoreException (Code code , String message ) {
108- this (code , message , null );
105+ public DatastoreException (DatastoreError error , String message ) {
106+ this (error , message , null );
109107 }
110108
111109 /**
112- * Returns the code associated with this exception.
110+ * Returns the DatastoreError associated with this exception.
113111 */
114- public Code code () {
115- return code ;
112+ public DatastoreError datastoreError () {
113+ return error ;
116114 }
117115
118116 static DatastoreException translateAndThrow (RetryHelperException ex ) {
@@ -122,35 +120,36 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
122120 if (ex instanceof RetryHelper .RetryInterruptedException ) {
123121 RetryHelper .RetryInterruptedException .propagate ();
124122 }
125- throw new DatastoreException (Code .UNKNOWN , ex .getMessage (), ex );
123+ throw new DatastoreException (DatastoreError .UNKNOWN , ex .getMessage (), ex );
126124 }
127125
128126 /**
129- * Translate DatastoreException to DatastoreException based on their
127+ * Translate DatastoreRpcExceptions to DatastoreExceptions based on their
130128 * HTTP error codes. This method will always throw a new DatastoreException.
131129 *
132130 * @throws DatastoreException every time
133131 */
134132 static DatastoreException translateAndThrow (DatastoreRpcException exception ) {
135133 String message = exception .getMessage ();
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 );
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 );
139138 }
140- throw code .translate (exception , message );
139+ throw error .translate (exception , message );
141140 }
142141
143142 /**
144- * Throw a DatastoreException with {@code FAILED_PRECONDITION} code and the {@code message}
143+ * Throw a DatastoreException with {@code FAILED_PRECONDITION} error and the {@code message}
145144 * in a nested exception.
146145 *
147146 * @throws DatastoreException every time
148147 */
149148 static DatastoreException throwInvalidRequest (String massage , Object ... params ) {
150- throw new DatastoreException (Code .FAILED_PRECONDITION , String .format (massage , params ));
149+ throw new DatastoreException (DatastoreError .FAILED_PRECONDITION , String .format (massage , params ));
151150 }
152151
153152 static DatastoreException propagateUserException (Exception ex ) {
154- throw new DatastoreException (Code .UNKNOWN , ex .getMessage (), ex );
153+ throw new DatastoreException (DatastoreError .UNKNOWN , ex .getMessage (), ex );
155154 }
156155}
0 commit comments