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 , ErrorInfo > REASON_TO_CODE ;
34+ private static final ImmutableMap <Integer , ErrorInfo > HTTP_TO_CODE ;
3435
35- private final Code code ;
36+ private final ErrorInfo errorInfo ;
3637
3738 /**
38- * An error code to represent the failure .
39+ * Represent metadata about {@link DatastoreException}s .
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 ErrorInfo {
4445
4546 ABORTED (Reason .ABORTED ),
4647 DEADLINE_EXCEEDED (Reason .DEADLINE_EXCEEDED ),
@@ -57,11 +58,11 @@ public enum Code {
5758 private final String description ;
5859 private final int httpStatus ;
5960
60- Code (Reason reason ) {
61+ ErrorInfo (Reason reason ) {
6162 this (reason .retryable (), reason .description (), reason .httpStatus ());
6263 }
6364
64- Code (boolean retryable , String description , int httpStatus ) {
65+ ErrorInfo (boolean retryable , String description , int httpStatus ) {
6566 this .retryable = retryable ;
6667 this .description = description ;
6768 this .httpStatus = httpStatus ;
@@ -89,30 +90,31 @@ DatastoreException translate(DatastoreRpcException exception, String message) {
8990 }
9091
9192 static {
92- ImmutableMap .Builder <String , Code > builder = ImmutableMap .builder ();
93- Map <Integer , Code > httpCodes = new HashMap <>();
94- for (Code code : Code .values ()) {
93+ ImmutableMap .Builder <String , ErrorInfo > builder = ImmutableMap .builder ();
94+ Map <Integer , ErrorInfo > httpCodes = new HashMap <>();
95+ for (ErrorInfo code : ErrorInfo .values ()) {
9596 builder .put (code .name (), code );
9697 httpCodes .put (code .httpStatus (), code );
9798 }
9899 REASON_TO_CODE = builder .build ();
99100 HTTP_TO_CODE = ImmutableMap .copyOf (httpCodes );
100101 }
101102
102- public DatastoreException (Code code , String message , Exception cause ) {
103- super (MoreObjects .firstNonNull (message , code .description ), cause );
104- this .code = code ;
103+ public DatastoreException (ErrorInfo errorInfo , String message , Exception cause ) {
104+ super (errorInfo .httpStatus (), MoreObjects .firstNonNull (message , errorInfo .description ),
105+ errorInfo .retryable (), cause );
106+ this .errorInfo = errorInfo ;
105107 }
106108
107- public DatastoreException (Code code , String message ) {
108- this (code , message , null );
109+ public DatastoreException (ErrorInfo errorInfo , String message ) {
110+ this (errorInfo , message , null );
109111 }
110112
111113 /**
112114 * Returns the code associated with this exception.
113115 */
114- public Code code () {
115- return code ;
116+ public ErrorInfo errorInfo () {
117+ return errorInfo ;
116118 }
117119
118120 static DatastoreException translateAndThrow (RetryHelperException ex ) {
@@ -122,7 +124,7 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
122124 if (ex instanceof RetryHelper .RetryInterruptedException ) {
123125 RetryHelper .RetryInterruptedException .propagate ();
124126 }
125- throw new DatastoreException (Code .UNKNOWN , ex .getMessage (), ex );
127+ throw new DatastoreException (ErrorInfo .UNKNOWN , ex .getMessage (), ex );
126128 }
127129
128130 /**
@@ -133,9 +135,9 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
133135 */
134136 static DatastoreException translateAndThrow (DatastoreRpcException exception ) {
135137 String message = exception .getMessage ();
136- Code code = REASON_TO_CODE .get (exception .reason ());
138+ ErrorInfo code = REASON_TO_CODE .get (exception .reason ());
137139 if (code == null ) {
138- code = MoreObjects .firstNonNull (HTTP_TO_CODE .get (exception .httpStatus ()), Code .UNKNOWN );
140+ code = MoreObjects .firstNonNull (HTTP_TO_CODE .get (exception .httpStatus ()), ErrorInfo .UNKNOWN );
139141 }
140142 throw code .translate (exception , message );
141143 }
@@ -147,10 +149,10 @@ static DatastoreException translateAndThrow(DatastoreRpcException exception) {
147149 * @throws DatastoreException every time
148150 */
149151 static DatastoreException throwInvalidRequest (String massage , Object ... params ) {
150- throw new DatastoreException (Code .FAILED_PRECONDITION , String .format (massage , params ));
152+ throw new DatastoreException (ErrorInfo .FAILED_PRECONDITION , String .format (massage , params ));
151153 }
152154
153155 static DatastoreException propagateUserException (Exception ex ) {
154- throw new DatastoreException (Code .UNKNOWN , ex .getMessage (), ex );
156+ throw new DatastoreException (ErrorInfo .UNKNOWN , ex .getMessage (), ex );
155157 }
156158}
0 commit comments