Skip to content

Commit ae29724

Browse files
committed
chore: add x-goog-request-id insertion into *Exception
Allows users to examine and report the requestId in any thrown exceptions. Updates googleapis#3537
1 parent e97b92e commit ae29724

18 files changed

+152
-62
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AbortedException.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ public class AbortedException extends SpannerException {
3535
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
3636
AbortedException(
3737
DoNotConstructDirectly token, @Nullable String message, @Nullable Throwable cause) {
38-
this(token, message, cause, null);
38+
this(token, message, cause, null, null);
3939
}
4040

4141
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
4242
AbortedException(
4343
DoNotConstructDirectly token,
4444
@Nullable String message,
4545
@Nullable Throwable cause,
46-
@Nullable ApiException apiException) {
47-
super(token, ErrorCode.ABORTED, IS_RETRYABLE, message, cause, apiException);
46+
@Nullable ApiException apiException,
47+
@Nullable XGoogSpannerRequestId reqId) {
48+
super(token, ErrorCode.ABORTED, IS_RETRYABLE, message, cause, apiException, reqId);
4849
}
4950

5051
/**

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AdminRequestsPerMinuteExceededException.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ public class AdminRequestsPerMinuteExceededException extends SpannerException {
3232
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
3333
AdminRequestsPerMinuteExceededException(
3434
DoNotConstructDirectly token, @Nullable String message, @Nullable Throwable cause) {
35-
this(token, message, cause, null);
35+
this(token, message, cause, null, null);
3636
}
3737

3838
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
3939
AdminRequestsPerMinuteExceededException(
4040
DoNotConstructDirectly token,
4141
@Nullable String message,
4242
@Nullable Throwable cause,
43-
@Nullable ApiException apiException) {
44-
super(token, ErrorCode.RESOURCE_EXHAUSTED, true, message, cause, apiException);
43+
@Nullable ApiException apiException,
44+
@Nullable XGoogSpannerRequestId reqId) {
45+
super(token, ErrorCode.RESOURCE_EXHAUSTED, true, message, cause, apiException, reqId);
4546
}
4647
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseNotFoundException.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class DatabaseNotFoundException extends ResourceNotFoundException {
3535
@Nullable String message,
3636
ResourceInfo resourceInfo,
3737
@Nullable Throwable cause) {
38-
this(token, message, resourceInfo, cause, null);
38+
this(token, message, resourceInfo, cause, null, null);
3939
}
4040

4141
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
@@ -44,7 +44,8 @@ public class DatabaseNotFoundException extends ResourceNotFoundException {
4444
@Nullable String message,
4545
ResourceInfo resourceInfo,
4646
@Nullable Throwable cause,
47-
@Nullable ApiException apiException) {
48-
super(token, message, resourceInfo, cause, apiException);
47+
@Nullable ApiException apiException,
48+
@Nullable XGoogSpannerRequestId reqId) {
49+
super(token, message, resourceInfo, cause, apiException, reqId);
4950
}
5051
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/InstanceNotFoundException.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ public class InstanceNotFoundException extends ResourceNotFoundException {
3535
@Nullable String message,
3636
ResourceInfo resourceInfo,
3737
@Nullable Throwable cause) {
38-
this(token, message, resourceInfo, cause, null);
38+
this(token, message, resourceInfo, cause, null, null);
3939
}
4040
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
4141
InstanceNotFoundException(
4242
DoNotConstructDirectly token,
4343
@Nullable String message,
4444
ResourceInfo resourceInfo,
4545
@Nullable Throwable cause,
46-
@Nullable ApiException apiException) {
47-
super(token, message, resourceInfo, cause, apiException);
46+
@Nullable ApiException apiException,
47+
@Nullable XGoogSpannerRequestId reqId) {
48+
super(token, message, resourceInfo, cause, apiException, reqId);
4849
}
4950
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/MissingDefaultSequenceKindException.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public class MissingDefaultSequenceKindException extends SpannerException {
3737
ErrorCode errorCode,
3838
String message,
3939
Throwable cause,
40-
@Nullable ApiException apiException) {
41-
super(token, errorCode, /*retryable = */ false, message, cause, apiException);
40+
@Nullable ApiException apiException,
41+
@Nullable XGoogSpannerRequestId reqId) {
42+
super(token, errorCode, /*retryable = */ false, message, cause, apiException, reqId);
4243
}
4344

4445
static boolean isMissingDefaultSequenceKindException(Throwable cause) {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static <R, M> Operation<R, M> failed(
8989
SpannerRpc rpc, String name, Status status, M metadata, Parser<R, M> parser, ApiClock clock) {
9090
SpannerException e =
9191
SpannerExceptionFactory.newSpannerException(
92-
ErrorCode.fromRpcStatus(status), status.getMessage(), null);
92+
ErrorCode.fromRpcStatus(status), status.getMessage(), (Throwable) (null));
9393
return new Operation<>(rpc, name, metadata, null, e, true, parser, clock);
9494
}
9595

google-cloud-spanner/src/main/java/com/google/cloud/spanner/ResumableStreamIterator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ private void backoffSleep(Context context, long backoffMillis) throws SpannerExc
180180
}
181181
if (latch.await(backoffMillis, TimeUnit.MILLISECONDS)) {
182182
// Woken by context cancellation.
183-
throw newSpannerExceptionForCancellation(context, null);
183+
throw newSpannerExceptionForCancellation(context, null, null /*TODO: requestId*/);
184184
}
185185
} catch (InterruptedException interruptExcept) {
186-
throw newSpannerExceptionForCancellation(context, interruptExcept);
186+
throw newSpannerExceptionForCancellation(context, interruptExcept, null /*TODO: requestId*/);
187187
} finally {
188188
context.removeListener(listener);
189189
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionNotFoundException.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SessionNotFoundException extends ResourceNotFoundException {
3535
@Nullable String message,
3636
ResourceInfo resourceInfo,
3737
@Nullable Throwable cause) {
38-
this(token, message, resourceInfo, cause, null);
38+
this(token, message, resourceInfo, cause, null, null);
3939
}
4040

4141
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
@@ -44,7 +44,8 @@ public class SessionNotFoundException extends ResourceNotFoundException {
4444
@Nullable String message,
4545
ResourceInfo resourceInfo,
4646
@Nullable Throwable cause,
47-
@Nullable ApiException apiException) {
48-
super(token, message, resourceInfo, cause, apiException);
47+
@Nullable ApiException apiException,
48+
@Nullable XGoogSpannerRequestId reqId) {
49+
super(token, message, resourceInfo, cause, apiException, reqId);
4950
}
5051
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerApiFutures.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public static <T> T getOrNull(ApiFuture<T> future) throws SpannerException {
3535
}
3636
throw SpannerExceptionFactory.newSpannerException(e.getCause());
3737
} catch (InterruptedException e) {
38-
throw SpannerExceptionFactory.propagateInterrupt(e);
38+
throw SpannerExceptionFactory.propagateInterrupt(e, null /*TODO: requestId*/);
3939
} catch (CancellationException e) {
40-
throw SpannerExceptionFactory.newSpannerExceptionForCancellation(null, e);
40+
throw SpannerExceptionFactory.newSpannerExceptionForCancellation(
41+
null, e, null /*TODO: requestId*/);
4142
}
4243
}
4344
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerBatchUpdateException.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public class SpannerBatchUpdateException extends SpannerException {
2525
ErrorCode code,
2626
String message,
2727
long[] counts,
28-
Throwable cause) {
29-
super(token, code, false, message, cause);
28+
Throwable cause,
29+
XGoogSpannerRequestId reqId) {
30+
super(token, code, false, message, cause, null, reqId);
3031
updateCounts = counts;
3132
}
3233

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerException.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public abstract static class ResourceNotFoundException extends SpannerException
4040
@Nullable String message,
4141
ResourceInfo resourceInfo,
4242
@Nullable Throwable cause,
43-
@Nullable ApiException apiException) {
44-
super(token, ErrorCode.NOT_FOUND, /* retryable */ false, message, cause, apiException);
43+
@Nullable ApiException apiException,
44+
@Nullable XGoogSpannerRequestId reqId) {
45+
super(token, ErrorCode.NOT_FOUND, /* retryable */ false, message, cause, apiException, reqId);
4546
this.resourceInfo = resourceInfo;
4647
}
4748

@@ -56,6 +57,7 @@ public String getResourceName() {
5657

5758
private final ErrorCode code;
5859
private final ApiException apiException;
60+
private final XGoogSpannerRequestId requestId;
5961

6062
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
6163
SpannerException(
@@ -75,19 +77,39 @@ public String getResourceName() {
7577
@Nullable String message,
7678
@Nullable Throwable cause,
7779
@Nullable ApiException apiException) {
80+
this(token, code, retryable, message, cause, apiException, null);
81+
}
82+
83+
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
84+
SpannerException(
85+
DoNotConstructDirectly token,
86+
ErrorCode code,
87+
boolean retryable,
88+
@Nullable String message,
89+
@Nullable Throwable cause,
90+
@Nullable ApiException apiException,
91+
@Nullable XGoogSpannerRequestId requestId) {
7892
super(message, cause, code.getCode(), retryable);
7993
if (token != DoNotConstructDirectly.ALLOWED) {
8094
throw new AssertionError("Do not construct directly: use SpannerExceptionFactory");
8195
}
8296
this.code = Preconditions.checkNotNull(code);
8397
this.apiException = apiException;
98+
this.requestId = requestId;
8499
}
85100

86101
/** Returns the error code associated with this exception. */
87102
public ErrorCode getErrorCode() {
88103
return code;
89104
}
90105

106+
public String getRequestId() {
107+
if (requestId == null) {
108+
return "";
109+
}
110+
return requestId.toString();
111+
}
112+
91113
enum DoNotConstructDirectly {
92114
ALLOWED
93115
}

0 commit comments

Comments
 (0)