Skip to content

Commit 410dbbc

Browse files
committed
refactor: make status extraction transport-neutral
Rename extractGrpcStatusCode to extractStatusCode and remove the io.grpc.Status dependency. The reason string on DatastoreException is already set from GAX's StatusCode.Code which supports both gRPC and HttpJson transports. Use a plain "UNKNOWN" string as fallback.
1 parent a4abccc commit 410dbbc

2 files changed

Lines changed: 11 additions & 25 deletions

File tree

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,16 @@ static DatastoreException propagateUserException(Exception ex) {
162162
}
163163

164164
/**
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.
165+
* Extracts the status code name from the given throwable. Walks the exception cause chain looking
166+
* for a {@link DatastoreException} that carries a reason string representing the status code
167+
* (e.g. "ABORTED", "UNAVAILABLE"). The reason is set from {@link
168+
* com.google.api.gax.rpc.StatusCode.Code} which is transport-neutral, supporting both gRPC and
169+
* HttpJson. Falls back to "UNKNOWN" if the status cannot be determined.
169170
*
170-
* @param throwable the throwable to extract the gRPC status code from
171-
* @return the gRPC status code name, or "UNKNOWN" if not determinable
171+
* @param throwable the throwable to extract the status code from
172+
* @return the status code name, or "UNKNOWN" if not determinable
172173
*/
173-
static String extractGrpcStatusCode(Throwable throwable) {
174+
static String extractStatusCode(Throwable throwable) {
174175
Throwable current = throwable;
175176
while (current != null) {
176177
if (current instanceof DatastoreException) {
@@ -181,6 +182,6 @@ static String extractGrpcStatusCode(Throwable throwable) {
181182
}
182183
current = current.getCause();
183184
}
184-
return io.grpc.Status.Code.UNKNOWN.toString();
185+
return "UNKNOWN";
185186
}
186187
}

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public T call() throws DatastoreException {
220220
return value;
221221
} catch (Exception ex) {
222222
transaction.rollback();
223-
recordAttempt(DatastoreException.extractGrpcStatusCode(ex));
223+
recordAttempt(DatastoreException.extractStatusCode(ex));
224224
throw DatastoreException.propagateUserException(ex);
225225
} finally {
226226
if (transaction.isActive()) {
@@ -287,7 +287,7 @@ public <T> T runInTransaction(
287287
TRANSACTION_EXCEPTION_HANDLER,
288288
getOptions().getClock());
289289
} catch (RetryHelperException e) {
290-
status = extractGrpcStatusCode(e);
290+
status = DatastoreException.extractStatusCode(e);
291291
span.end(e);
292292
throw DatastoreException.translateAndThrow(e);
293293
} finally {
@@ -302,21 +302,6 @@ public <T> T runInTransaction(
302302
}
303303
}
304304

305-
/**
306-
* Extracts the gRPC status code from a {@link RetryHelperException}. The underlying cause is
307-
* expected to be a {@link DatastoreException} which carries the gRPC reason. Falls back to
308-
* "UNKNOWN" if the status cannot be determined.
309-
*/
310-
private static String extractGrpcStatusCode(RetryHelperException e) {
311-
Throwable cause = e.getCause();
312-
if (cause instanceof DatastoreException) {
313-
String reason = ((DatastoreException) cause).getReason();
314-
if (reason != null && !reason.isEmpty()) {
315-
return reason;
316-
}
317-
}
318-
return "UNKNOWN";
319-
}
320305

321306
@Override
322307
public <T> QueryResults<T> run(Query<T> query) {

0 commit comments

Comments
 (0)