From a6ac5c5c46351fb6d99ba19d60cd3a91d303dacb Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 4 Nov 2015 19:06:58 -0800 Subject: [PATCH] Rename enum, fix docs, and make accessors package private --- .../google/gcloud/BaseServiceException.java | 5 +- .../gcloud/datastore/DatastoreException.java | 48 +++++++++---------- .../datastore/DatastoreExceptionTest.java | 12 ++--- .../gcloud/datastore/DatastoreTest.java | 4 +- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java index 45c047d4710a..cd0933426756 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java @@ -17,7 +17,7 @@ package com.google.gcloud; /** - * Base service exception. + * Base class for all service exceptions. */ public class BaseServiceException extends RuntimeException { @@ -45,6 +45,9 @@ public int code() { return code; } + /** + * Returns {@code true} when it is safe to retry the operation that caused this exception. + */ public boolean retryable() { return retryable; } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java index ecbdd57b6bfb..3c050728eb57 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java @@ -30,10 +30,10 @@ public class DatastoreException extends BaseServiceException { private static final long serialVersionUID = -2336749234060754893L; - private static final ImmutableMap REASON_TO_CODE; - private static final ImmutableMap HTTP_TO_CODE; + private static final ImmutableMap REASON_TO_CODE; + private static final ImmutableMap HTTP_TO_CODE; - private final ErrorInfo errorInfo; + private final DatastoreError error; /** * Represent metadata about {@link DatastoreException}s. @@ -41,7 +41,7 @@ public class DatastoreException extends BaseServiceException { * @see Google Cloud * Datastore error codes */ - public enum ErrorInfo { + public enum DatastoreError { ABORTED(Reason.ABORTED), DEADLINE_EXCEEDED(Reason.DEADLINE_EXCEEDED), @@ -58,29 +58,25 @@ public enum ErrorInfo { private final String description; private final int httpStatus; - ErrorInfo(Reason reason) { + DatastoreError(Reason reason) { this(reason.retryable(), reason.description(), reason.httpStatus()); } - ErrorInfo(boolean retryable, String description, int httpStatus) { + DatastoreError(boolean retryable, String description, int httpStatus) { this.retryable = retryable; this.description = description; this.httpStatus = httpStatus; } - public String description() { + String description() { return description; } - public int httpStatus() { + int httpStatus() { return httpStatus; } - /** - * Returns {@code true} if this exception is transient and the same request could be retried. - * For any retry it is highly recommended to apply an exponential backoff. - */ - public boolean retryable() { + boolean retryable() { return retryable; } @@ -90,9 +86,9 @@ DatastoreException translate(DatastoreRpcException exception, String message) { } static { - ImmutableMap.Builder builder = ImmutableMap.builder(); - Map httpCodes = new HashMap<>(); - for (ErrorInfo code : ErrorInfo.values()) { + ImmutableMap.Builder builder = ImmutableMap.builder(); + Map httpCodes = new HashMap<>(); + for (DatastoreError code : DatastoreError.values()) { builder.put(code.name(), code); httpCodes.put(code.httpStatus(), code); } @@ -100,21 +96,21 @@ DatastoreException translate(DatastoreRpcException exception, String message) { HTTP_TO_CODE = ImmutableMap.copyOf(httpCodes); } - public DatastoreException(ErrorInfo errorInfo, String message, Exception cause) { + public DatastoreException(DatastoreError errorInfo, String message, Exception cause) { super(errorInfo.httpStatus(), MoreObjects.firstNonNull(message, errorInfo.description), errorInfo.retryable(), cause); - this.errorInfo = errorInfo; + this.error = errorInfo; } - public DatastoreException(ErrorInfo errorInfo, String message) { + public DatastoreException(DatastoreError errorInfo, String message) { this(errorInfo, message, null); } /** * Returns the code associated with this exception. */ - public ErrorInfo errorInfo() { - return errorInfo; + public DatastoreError datastoreError() { + return error; } static DatastoreException translateAndThrow(RetryHelperException ex) { @@ -124,7 +120,7 @@ static DatastoreException translateAndThrow(RetryHelperException ex) { if (ex instanceof RetryHelper.RetryInterruptedException) { RetryHelper.RetryInterruptedException.propagate(); } - throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex); + throw new DatastoreException(DatastoreError.UNKNOWN, ex.getMessage(), ex); } /** @@ -135,9 +131,9 @@ static DatastoreException translateAndThrow(RetryHelperException ex) { */ static DatastoreException translateAndThrow(DatastoreRpcException exception) { String message = exception.getMessage(); - ErrorInfo code = REASON_TO_CODE.get(exception.reason()); + DatastoreError code = REASON_TO_CODE.get(exception.reason()); if (code == null) { - code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), ErrorInfo.UNKNOWN); + code = MoreObjects.firstNonNull(HTTP_TO_CODE.get(exception.httpStatus()), DatastoreError.UNKNOWN); } throw code.translate(exception, message); } @@ -149,10 +145,10 @@ static DatastoreException translateAndThrow(DatastoreRpcException exception) { * @throws DatastoreException every time */ static DatastoreException throwInvalidRequest(String massage, Object... params) { - throw new DatastoreException(ErrorInfo.FAILED_PRECONDITION, String.format(massage, params)); + throw new DatastoreException(DatastoreError.FAILED_PRECONDITION, String.format(massage, params)); } static DatastoreException propagateUserException(Exception ex) { - throw new DatastoreException(ErrorInfo.UNKNOWN, ex.getMessage(), ex); + throw new DatastoreException(DatastoreError.UNKNOWN, ex.getMessage(), ex); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java index 20c2f742579a..03a5b90cef17 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreExceptionTest.java @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import com.google.gcloud.datastore.DatastoreException.ErrorInfo; +import com.google.gcloud.datastore.DatastoreException.DatastoreError; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException; import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason; @@ -30,14 +30,14 @@ public class DatastoreExceptionTest { @Test public void testCode() throws Exception { for (Reason reason : Reason.values()) { - ErrorInfo code = ErrorInfo.valueOf(reason.name()); + DatastoreError code = DatastoreError.valueOf(reason.name()); assertEquals(reason.retryable(), code.retryable()); assertEquals(reason.description(), code.description()); assertEquals(reason.httpStatus(), code.httpStatus()); } - DatastoreException exception = new DatastoreException(ErrorInfo.ABORTED, "bla"); - assertEquals(ErrorInfo.ABORTED, exception.errorInfo()); + DatastoreException exception = new DatastoreException(DatastoreError.ABORTED, "bla"); + assertEquals(DatastoreError.ABORTED, exception.datastoreError()); } @Test @@ -47,7 +47,7 @@ public void testTranslateAndThrow() throws Exception { DatastoreException.translateAndThrow(new DatastoreRpcException(reason)); fail("Exception expected"); } catch (DatastoreException ex) { - assertEquals(reason.name(), ex.errorInfo().name()); + assertEquals(reason.name(), ex.datastoreError().name()); } } } @@ -58,7 +58,7 @@ public void testThrowInvalidRequest() throws Exception { DatastoreException.throwInvalidRequest("message %s %d", "a", 1); fail("Exception expected"); } catch (DatastoreException ex) { - assertEquals(ErrorInfo.FAILED_PRECONDITION, ex.errorInfo()); + assertEquals(DatastoreError.FAILED_PRECONDITION, ex.datastoreError()); assertEquals("message a 1", ex.getMessage()); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 9b42c99b0da1..6afd40927264 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -197,7 +197,7 @@ public void testTransactionWithRead() { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { - assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo()); + assertEquals(DatastoreException.DatastoreError.ABORTED, expected.datastoreError()); } } @@ -225,7 +225,7 @@ public void testTransactionWithQuery() { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { - assertEquals(DatastoreException.ErrorInfo.ABORTED, expected.errorInfo()); + assertEquals(DatastoreException.DatastoreError.ABORTED, expected.datastoreError()); } }