3030public class DatastoreException extends BaseServiceException {
3131
3232 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 ;
33+ private static final ImmutableMap <String , DatastoreError > REASON_TO_CODE ;
34+ private static final ImmutableMap <Integer , DatastoreError > HTTP_TO_CODE ;
3535
36- private final ErrorInfo errorInfo ;
36+ private final DatastoreError error ;
3737
3838 /**
39- * Represent metadata about {@link DatastoreException}s .
39+ * Represents Datastore errors .
4040 *
4141 * @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud
4242 * Datastore error codes</a>
4343 */
44- public enum ErrorInfo {
44+ public enum DatastoreError {
4545
4646 ABORTED (Reason .ABORTED ),
4747 DEADLINE_EXCEEDED (Reason .DEADLINE_EXCEEDED ),
@@ -58,29 +58,25 @@ public enum ErrorInfo {
5858 private final String description ;
5959 private final int httpStatus ;
6060
61- ErrorInfo (Reason reason ) {
61+ DatastoreError (Reason reason ) {
6262 this (reason .retryable (), reason .description (), reason .httpStatus ());
6363 }
6464
65- ErrorInfo (boolean retryable , String description , int httpStatus ) {
65+ DatastoreError (boolean retryable , String description , int httpStatus ) {
6666 this .retryable = retryable ;
6767 this .description = description ;
6868 this .httpStatus = httpStatus ;
6969 }
7070
71- public String description () {
71+ String description () {
7272 return description ;
7373 }
7474
75- public int httpStatus () {
75+ int httpStatus () {
7676 return httpStatus ;
7777 }
7878
79- /**
80- * Returns {@code true} if this exception is transient and the same request could be retried.
81- * For any retry it is highly recommended to apply an exponential backoff.
82- */
83- public boolean retryable () {
79+ boolean retryable () {
8480 return retryable ;
8581 }
8682
@@ -90,31 +86,31 @@ DatastoreException translate(DatastoreRpcException exception, String message) {
9086 }
9187
9288 static {
93- ImmutableMap .Builder <String , ErrorInfo > builder = ImmutableMap .builder ();
94- Map <Integer , ErrorInfo > httpCodes = new HashMap <>();
95- for (ErrorInfo code : ErrorInfo .values ()) {
89+ ImmutableMap .Builder <String , DatastoreError > builder = ImmutableMap .builder ();
90+ Map <Integer , DatastoreError > httpCodes = new HashMap <>();
91+ for (DatastoreError code : DatastoreError .values ()) {
9692 builder .put (code .name (), code );
9793 httpCodes .put (code .httpStatus (), code );
9894 }
9995 REASON_TO_CODE = builder .build ();
10096 HTTP_TO_CODE = ImmutableMap .copyOf (httpCodes );
10197 }
10298
103- public DatastoreException (ErrorInfo errorInfo , String message , Exception cause ) {
99+ public DatastoreException (DatastoreError errorInfo , String message , Exception cause ) {
104100 super (errorInfo .httpStatus (), MoreObjects .firstNonNull (message , errorInfo .description ),
105101 errorInfo .retryable (), cause );
106- this .errorInfo = errorInfo ;
102+ this .error = errorInfo ;
107103 }
108104
109- public DatastoreException (ErrorInfo errorInfo , String message ) {
105+ public DatastoreException (DatastoreError errorInfo , String message ) {
110106 this (errorInfo , message , null );
111107 }
112108
113109 /**
114110 * Returns the code associated with this exception.
115111 */
116- public ErrorInfo errorInfo () {
117- return errorInfo ;
112+ public DatastoreError datastoreError () {
113+ return error ;
118114 }
119115
120116 static DatastoreException translateAndThrow (RetryHelperException ex ) {
@@ -124,7 +120,7 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
124120 if (ex instanceof RetryHelper .RetryInterruptedException ) {
125121 RetryHelper .RetryInterruptedException .propagate ();
126122 }
127- throw new DatastoreException (ErrorInfo .UNKNOWN , ex .getMessage (), ex );
123+ throw new DatastoreException (DatastoreError .UNKNOWN , ex .getMessage (), ex );
128124 }
129125
130126 /**
@@ -135,9 +131,9 @@ static DatastoreException translateAndThrow(RetryHelperException ex) {
135131 */
136132 static DatastoreException translateAndThrow (DatastoreRpcException exception ) {
137133 String message = exception .getMessage ();
138- ErrorInfo code = REASON_TO_CODE .get (exception .reason ());
134+ DatastoreError code = REASON_TO_CODE .get (exception .reason ());
139135 if (code == null ) {
140- code = MoreObjects .firstNonNull (HTTP_TO_CODE .get (exception .httpStatus ()), ErrorInfo .UNKNOWN );
136+ code = MoreObjects .firstNonNull (HTTP_TO_CODE .get (exception .httpStatus ()), DatastoreError .UNKNOWN );
141137 }
142138 throw code .translate (exception , message );
143139 }
@@ -149,10 +145,10 @@ static DatastoreException translateAndThrow(DatastoreRpcException exception) {
149145 * @throws DatastoreException every time
150146 */
151147 static DatastoreException throwInvalidRequest (String massage , Object ... params ) {
152- throw new DatastoreException (ErrorInfo .FAILED_PRECONDITION , String .format (massage , params ));
148+ throw new DatastoreException (DatastoreError .FAILED_PRECONDITION , String .format (massage , params ));
153149 }
154150
155151 static DatastoreException propagateUserException (Exception ex ) {
156- throw new DatastoreException (ErrorInfo .UNKNOWN , ex .getMessage (), ex );
152+ throw new DatastoreException (DatastoreError .UNKNOWN , ex .getMessage (), ex );
157153 }
158154}
0 commit comments