Skip to content

Commit 2d6466f

Browse files
committed
More newSpannerException updates
1 parent a183de7 commit 2d6466f

File tree

8 files changed

+37
-27
lines changed

8 files changed

+37
-27
lines changed

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

Lines changed: 2 additions & 2 deletions
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/SpannerApiFutures.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static <T> T getOrNull(ApiFuture<T> future) throws SpannerException {
3737
} catch (InterruptedException e) {
3838
throw SpannerExceptionFactory.propagateInterrupt(e);
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

Lines changed: 3 additions & 2 deletions
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/SpannerExceptionFactory.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,23 @@ public static SpannerException asSpannerException(Throwable t) {
118118
* #newSpannerException(ErrorCode, String)} instead of this method.
119119
*/
120120
public static SpannerException newSpannerException(Throwable cause) {
121-
return newSpannerException(null, cause);
121+
return newSpannerException(null, cause, null);
122122
}
123123

124124
public static SpannerException newSpannerException(
125125
Throwable cause, XGoogSpannerRequestId requestId) {
126-
return newSpannerExceptionPreformatted(null, null, cause, null, requestId);
126+
return newSpannerException(null, cause, requestId);
127127
}
128128

129129
public static SpannerBatchUpdateException newSpannerBatchUpdateException(
130-
ErrorCode code, String message, long[] updateCounts) {
130+
ErrorCode code, String message, long[] updateCounts, @Nullable XGoogSpannerRequestId reqId) {
131131
DoNotConstructDirectly token = DoNotConstructDirectly.ALLOWED;
132132
SpannerException cause = null;
133133
if (isTransactionMutationLimitException(code, message)) {
134-
cause = new TransactionMutationLimitExceededException(token, code, message, null, null, null);
134+
cause =
135+
new TransactionMutationLimitExceededException(token, code, message, null, null, reqId);
135136
}
136-
return new SpannerBatchUpdateException(token, code, message, updateCounts, cause);
137+
return new SpannerBatchUpdateException(token, code, message, updateCounts, cause, reqId);
137138
}
138139

139140
/** Constructs a specific error that */
@@ -191,21 +192,22 @@ public static SpannerBatchUpdateException newSpannerBatchUpdateException(
191192
* <p>Intended for internal library use; user code should use {@link
192193
* #newSpannerException(ErrorCode, String)} instead of this method.
193194
*/
194-
public static SpannerException newSpannerException(@Nullable Context context, Throwable cause) {
195+
public static SpannerException newSpannerException(
196+
@Nullable Context context, Throwable cause, @Nullable XGoogSpannerRequestId reqId) {
195197
if (cause instanceof SpannerException) {
196198
SpannerException e = (SpannerException) cause;
197-
return newSpannerExceptionPreformatted(e.getErrorCode(), e.getMessage(), e);
199+
return newSpannerExceptionPreformatted(e.getErrorCode(), e.getMessage(), e, null, reqId);
198200
} else if (cause instanceof CancellationException) {
199-
return newSpannerExceptionForCancellation(context, cause);
201+
return newSpannerExceptionForCancellation(context, cause, reqId);
200202
} else if (cause instanceof ApiException) {
201-
return fromApiException((ApiException) cause);
203+
return fromApiException((ApiException) cause, reqId);
202204
}
203205
// Extract gRPC status. This will produce "UNKNOWN" for non-gRPC exceptions.
204206
Status status = Status.fromThrowable(cause);
205207
if (status.getCode() == Status.Code.CANCELLED) {
206-
return newSpannerExceptionForCancellation(context, cause);
208+
return newSpannerExceptionForCancellation(context, cause, reqId);
207209
}
208-
return newSpannerException(ErrorCode.fromGrpcStatus(status), cause.getMessage(), cause);
210+
return newSpannerException(ErrorCode.fromGrpcStatus(status), cause.getMessage(), cause, reqId);
209211
}
210212

211213
public static RuntimeException causeAsRunTimeException(ExecutionException executionException) {
@@ -229,7 +231,7 @@ static SpannerException newRetryOnDifferentGrpcChannelException(
229231
}
230232

231233
static SpannerException newSpannerExceptionForCancellation(
232-
@Nullable Context context, @Nullable Throwable cause) {
234+
@Nullable Context context, @Nullable Throwable cause, @Nullable XGoogSpannerRequestId reqId) {
233235
if (context != null && context.isCancelled()) {
234236
Throwable cancellationCause = context.cancellationCause();
235237
Throwable throwable =
@@ -238,9 +240,10 @@ static SpannerException newSpannerExceptionForCancellation(
238240
: MoreObjects.firstNonNull(cause, cancellationCause);
239241
if (cancellationCause instanceof TimeoutException) {
240242
return newSpannerException(
241-
ErrorCode.DEADLINE_EXCEEDED, "Current context exceeded deadline", throwable);
243+
ErrorCode.DEADLINE_EXCEEDED, "Current context exceeded deadline", throwable, reqId);
242244
} else {
243-
return newSpannerException(ErrorCode.CANCELLED, "Current context was cancelled", throwable);
245+
return newSpannerException(
246+
ErrorCode.CANCELLED, "Current context was cancelled", throwable, reqId);
244247
}
245248
}
246249
return newSpannerException(
@@ -371,7 +374,8 @@ static SpannerException newSpannerExceptionPreformatted(
371374
code, message, cause, null, (XGoogSpannerRequestId) (null));
372375
}
373376

374-
private static SpannerException fromApiException(ApiException exception) {
377+
private static SpannerException fromApiException(
378+
ApiException exception, @Nullable XGoogSpannerRequestId reqId) {
375379
Status.Code code;
376380
if (exception.getStatusCode() instanceof GrpcStatusCode) {
377381
code = ((GrpcStatusCode) exception.getStatusCode()).getTransportCode();
@@ -387,7 +391,7 @@ private static SpannerException fromApiException(ApiException exception) {
387391
formatMessage(errorCode, exception.getMessage()),
388392
exception.getCause(),
389393
exception,
390-
null);
394+
reqId);
391395
}
392396

393397
private static boolean isRetryable(ErrorCode code, @Nullable Throwable cause) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ public TimedAttemptSettings createNextAttempt(
120120
public boolean shouldRetry(Throwable prevThrowable, T prevResponse)
121121
throws CancellationException {
122122
if (Context.current().isCancelled()) {
123-
throw SpannerExceptionFactory.newSpannerExceptionForCancellation(Context.current(), null);
123+
throw SpannerExceptionFactory.newSpannerExceptionForCancellation(
124+
Context.current(), null, null);
124125
}
125126
return prevThrowable instanceof AbortedException
126127
|| prevThrowable instanceof com.google.api.gax.rpc.AbortedException;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ options, getPreviousTransactionId())))
685685
}
686686
throw se;
687687
} catch (InterruptedException e) {
688-
throw SpannerExceptionFactory.newSpannerExceptionForCancellation(null, e);
688+
throw SpannerExceptionFactory.newSpannerExceptionForCancellation(
689+
null, e, null /*TODO: requestId*/);
689690
}
690691
}
691692
// There is already a transactionId available. Include that id as the transaction to use.
@@ -1074,7 +1075,8 @@ public long[] batchUpdate(Iterable<Statement> statements, UpdateOption... update
10741075
throw newSpannerBatchUpdateException(
10751076
ErrorCode.fromRpcStatus(response.getStatus()),
10761077
response.getStatus().getMessage(),
1077-
results);
1078+
results,
1079+
null /*TODO: requestId*/);
10781080
}
10791081
return results;
10801082
} catch (Throwable e) {
@@ -1141,7 +1143,8 @@ public ApiFuture<long[]> batchUpdateAsync(
11411143
throw newSpannerBatchUpdateException(
11421144
ErrorCode.fromRpcStatus(batchDmlResponse.getStatus()),
11431145
batchDmlResponse.getStatus().getMessage(),
1144-
results);
1146+
results,
1147+
null /*TODO: requestId*/);
11451148
}
11461149
return results;
11471150
},

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/DdlBatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public ApiFuture<long[]> runBatchAsync(CallType callType) {
268268
} catch (SpannerException e) {
269269
long[] updateCounts = extractUpdateCounts(operationReference.get());
270270
throw SpannerExceptionFactory.newSpannerBatchUpdateException(
271-
e.getErrorCode(), e.getMessage(), updateCounts);
271+
e.getErrorCode(), e.getMessage(), updateCounts, null /* TODO: requestId */);
272272
}
273273
} catch (Throwable t) {
274274
span.setStatus(StatusCode.ERROR);

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,7 @@ private static <T> T get(final Future<T> future) throws SpannerException {
19741974
future.cancel(true);
19751975
throw SpannerExceptionFactory.propagateInterrupt(e);
19761976
} catch (Exception e) {
1977-
throw newSpannerException(context, e);
1977+
throw newSpannerException(context, e, null);
19781978
}
19791979
}
19801980

0 commit comments

Comments
 (0)