Skip to content

chore: add x-goog-request-id insertion into *Exception #3813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ public class AbortedException extends SpannerException {
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
AbortedException(
DoNotConstructDirectly token, @Nullable String message, @Nullable Throwable cause) {
this(token, message, cause, null);
this(token, message, cause, null, null);
}

/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
AbortedException(
DoNotConstructDirectly token,
@Nullable String message,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
super(token, ErrorCode.ABORTED, IS_RETRYABLE, message, cause, apiException);
@Nullable ApiException apiException,
@Nullable XGoogSpannerRequestId reqId) {
super(token, ErrorCode.ABORTED, IS_RETRYABLE, message, cause, apiException, reqId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ public class AdminRequestsPerMinuteExceededException extends SpannerException {
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
AdminRequestsPerMinuteExceededException(
DoNotConstructDirectly token, @Nullable String message, @Nullable Throwable cause) {
this(token, message, cause, null);
this(token, message, cause, null, null);
}

/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
AdminRequestsPerMinuteExceededException(
DoNotConstructDirectly token,
@Nullable String message,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
super(token, ErrorCode.RESOURCE_EXHAUSTED, true, message, cause, apiException);
@Nullable ApiException apiException,
@Nullable XGoogSpannerRequestId reqId) {
super(token, ErrorCode.RESOURCE_EXHAUSTED, true, message, cause, apiException, reqId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class DatabaseNotFoundException extends ResourceNotFoundException {
@Nullable String message,
ResourceInfo resourceInfo,
@Nullable Throwable cause) {
this(token, message, resourceInfo, cause, null);
this(token, message, resourceInfo, cause, null, null);
}

/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
Expand All @@ -44,7 +44,8 @@ public class DatabaseNotFoundException extends ResourceNotFoundException {
@Nullable String message,
ResourceInfo resourceInfo,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
super(token, message, resourceInfo, cause, apiException);
@Nullable ApiException apiException,
@Nullable XGoogSpannerRequestId reqId) {
super(token, message, resourceInfo, cause, apiException, reqId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ public class InstanceNotFoundException extends ResourceNotFoundException {
@Nullable String message,
ResourceInfo resourceInfo,
@Nullable Throwable cause) {
this(token, message, resourceInfo, cause, null);
this(token, message, resourceInfo, cause, null, null);
}
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
InstanceNotFoundException(
DoNotConstructDirectly token,
@Nullable String message,
ResourceInfo resourceInfo,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
super(token, message, resourceInfo, cause, apiException);
@Nullable ApiException apiException,
@Nullable XGoogSpannerRequestId reqId) {
super(token, message, resourceInfo, cause, apiException, reqId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public class MissingDefaultSequenceKindException extends SpannerException {
ErrorCode errorCode,
String message,
Throwable cause,
@Nullable ApiException apiException) {
super(token, errorCode, /*retryable = */ false, message, cause, apiException);
@Nullable ApiException apiException,
@Nullable XGoogSpannerRequestId reqId) {
super(token, errorCode, /*retryable = */ false, message, cause, apiException, reqId);
}

static boolean isMissingDefaultSequenceKindException(Throwable cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static <R, M> Operation<R, M> failed(
SpannerRpc rpc, String name, Status status, M metadata, Parser<R, M> parser, ApiClock clock) {
SpannerException e =
SpannerExceptionFactory.newSpannerException(
ErrorCode.fromRpcStatus(status), status.getMessage(), null);
ErrorCode.fromRpcStatus(status), status.getMessage(), (Throwable) (null));
return new Operation<>(rpc, name, metadata, null, e, true, parser, clock);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ private void backoffSleep(Context context, long backoffMillis) throws SpannerExc
}
if (latch.await(backoffMillis, TimeUnit.MILLISECONDS)) {
// Woken by context cancellation.
throw newSpannerExceptionForCancellation(context, null);
throw newSpannerExceptionForCancellation(context, null, null /*TODO: requestId*/);
}
} catch (InterruptedException interruptExcept) {
throw newSpannerExceptionForCancellation(context, interruptExcept);
throw newSpannerExceptionForCancellation(context, interruptExcept, null /*TODO: requestId*/);
} finally {
context.removeListener(listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SessionNotFoundException extends ResourceNotFoundException {
@Nullable String message,
ResourceInfo resourceInfo,
@Nullable Throwable cause) {
this(token, message, resourceInfo, cause, null);
this(token, message, resourceInfo, cause, null, null);
}

/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
Expand All @@ -44,7 +44,8 @@ public class SessionNotFoundException extends ResourceNotFoundException {
@Nullable String message,
ResourceInfo resourceInfo,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
super(token, message, resourceInfo, cause, apiException);
@Nullable ApiException apiException,
@Nullable XGoogSpannerRequestId reqId) {
super(token, message, resourceInfo, cause, apiException, reqId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public static <T> T getOrNull(ApiFuture<T> future) throws SpannerException {
}
throw SpannerExceptionFactory.newSpannerException(e.getCause());
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
throw SpannerExceptionFactory.propagateInterrupt(e, null /*TODO: requestId*/);
} catch (CancellationException e) {
throw SpannerExceptionFactory.newSpannerExceptionForCancellation(null, e);
throw SpannerExceptionFactory.newSpannerExceptionForCancellation(
null, e, null /*TODO: requestId*/);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public class SpannerBatchUpdateException extends SpannerException {
ErrorCode code,
String message,
long[] counts,
Throwable cause) {
super(token, code, false, message, cause);
Throwable cause,
XGoogSpannerRequestId reqId) {
super(token, code, false, message, cause, null, reqId);
updateCounts = counts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public abstract static class ResourceNotFoundException extends SpannerException
@Nullable String message,
ResourceInfo resourceInfo,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
super(token, ErrorCode.NOT_FOUND, /* retryable */ false, message, cause, apiException);
@Nullable ApiException apiException,
@Nullable XGoogSpannerRequestId reqId) {
super(token, ErrorCode.NOT_FOUND, /* retryable */ false, message, cause, apiException, reqId);
this.resourceInfo = resourceInfo;
}

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

private final ErrorCode code;
private final ApiException apiException;
private final XGoogSpannerRequestId requestId;

/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
SpannerException(
Expand All @@ -75,19 +77,39 @@ public String getResourceName() {
@Nullable String message,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
this(token, code, retryable, message, cause, apiException, null);
}

/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
SpannerException(
DoNotConstructDirectly token,
ErrorCode code,
boolean retryable,
@Nullable String message,
@Nullable Throwable cause,
@Nullable ApiException apiException,
@Nullable XGoogSpannerRequestId requestId) {
super(message, cause, code.getCode(), retryable);
if (token != DoNotConstructDirectly.ALLOWED) {
throw new AssertionError("Do not construct directly: use SpannerExceptionFactory");
}
this.code = Preconditions.checkNotNull(code);
this.apiException = apiException;
this.requestId = requestId;
}

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

public String getRequestId() {
if (requestId == null) {
return "";
}
return requestId.toString();
}

enum DoNotConstructDirectly {
ALLOWED
}
Expand Down
Loading
Loading