Skip to content

Commit a4abccc

Browse files
committed
refactor: consolidate gRPC status extraction into DatastoreException
Move extractStatus and extractGrpcStatusCode into a single shared DatastoreException.extractGrpcStatusCode(Throwable) method that walks the exception cause chain. Both call sites in DatastoreImpl now delegate to this shared method.
1 parent d140926 commit a4abccc

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,27 @@ static DatastoreException throwInvalidRequest(String massage, Object... params)
160160
static DatastoreException propagateUserException(Exception ex) {
161161
throw new DatastoreException(BaseServiceException.UNKNOWN_CODE, ex.getMessage(), null, ex);
162162
}
163+
164+
/**
165+
* Extracts the gRPC status code name from the given throwable. Walks the exception cause chain
166+
* looking for a {@link DatastoreException} that carries a gRPC reason string (e.g. "ABORTED",
167+
* "UNAVAILABLE"). Falls back to {@link io.grpc.Status.Code#UNKNOWN} if the status cannot be
168+
* determined.
169+
*
170+
* @param throwable the throwable to extract the gRPC status code from
171+
* @return the gRPC status code name, or "UNKNOWN" if not determinable
172+
*/
173+
static String extractGrpcStatusCode(Throwable throwable) {
174+
Throwable current = throwable;
175+
while (current != null) {
176+
if (current instanceof DatastoreException) {
177+
String reason = ((DatastoreException) current).getReason();
178+
if (reason != null && !reason.isEmpty()) {
179+
return reason;
180+
}
181+
}
182+
current = current.getCause();
183+
}
184+
return io.grpc.Status.Code.UNKNOWN.toString();
185+
}
163186
}

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_RUN_QUERY;
3838

3939
import com.google.api.core.BetaApi;
40+
import com.google.api.gax.grpc.GrpcStatusCode;
4041
import com.google.api.gax.retrying.RetrySettings;
4142
import com.google.cloud.BaseService;
4243
import com.google.cloud.ExceptionHandler;
@@ -64,6 +65,7 @@
6465
import com.google.datastore.v1.RunQueryResponse;
6566
import com.google.datastore.v1.TransactionOptions;
6667
import com.google.protobuf.ByteString;
68+
import io.grpc.Status;
6769
import io.opentelemetry.context.Context;
6870
import java.util.ArrayList;
6971
import java.util.Arrays;
@@ -214,11 +216,11 @@ public T call() throws DatastoreException {
214216
transaction = datastore.newTransaction(options);
215217
T value = callable.run(transaction);
216218
transaction.commit();
217-
recordAttempt("OK");
219+
recordAttempt(Status.Code.OK.toString());
218220
return value;
219221
} catch (Exception ex) {
220222
transaction.rollback();
221-
recordAttempt(extractStatus(ex));
223+
recordAttempt(DatastoreException.extractGrpcStatusCode(ex));
222224
throw DatastoreException.propagateUserException(ex);
223225
} finally {
224226
if (transaction.isActive()) {

0 commit comments

Comments
 (0)