GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Spanner: Add methods in SpannerExceptionFactory for propagating ApiException and TimeoutException by yihanzhen · Pull Request #4598 · googleapis/google-cloud-java

Spanner: Add methods in SpannerExceptionFactory for propagating ApiException and TimeoutException #4598

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.

Sign up for GitHub

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
yihanzhen merged 1 commit into googleapis:master from yihanzhen:spanner-exception-factory
Mar 1, 2019

Uh oh!

There was an error while loading. Please reload this page.

Merged

Spanner: Add methods in SpannerExceptionFactory for propagating ApiException and TimeoutException #4598

Changes from all commits
Commits
Show all changes
1 commit
Select commit
a5d255a
add propagateTimeout in SpannerExceptionFactory
yihanzhen Mar 1, 2019
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

37 changes: 37 additions & 0 deletions 37 .../google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerExceptionFactory.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static com.google.cloud.spanner.SpannerException.DoNotConstructDirectly;

import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.api.gax.rpc.ApiException;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicate;
import io.grpc.Context;
Expand Down Expand Up @@ -48,6 +50,28 @@ public static SpannerException propagateInterrupt(InterruptedException e) {
return SpannerExceptionFactory.newSpannerException(ErrorCode.CANCELLED, "Interrupted", e);
}

/**
* Transforms a {@code TimeoutException} to a {@code SpannerException}.
*
* <pre>
* <code>
* try {
* Spanner spanner = SpannerOptions.getDefaultInstance();
* spanner
* .getDatabaseAdminClient()
* .createDatabase("[INSTANCE_ID]", "[DATABASE_ID]", [STATEMENTS])
* .get();
* } catch (TimeoutException e) {
* propagateTimeout(e);
* }
* </code>
* </pre>
*/
public static SpannerException propagateTimeout(TimeoutException e) {
Comment thread

@sduskis sduskis Feb 28, 2019

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this supposed to be used?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@yihanzhen yihanzhen Feb 28, 2019

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used in the client library but by people who depend on spanner inside google3.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@sduskis sduskis Mar 1, 2019

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. Please add a comment to this method, and it might be good to give an example use.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
return SpannerExceptionFactory.newSpannerException(
ErrorCode.DEADLINE_EXCEEDED, "Operation did not complete in the given time", e);
}

/**
* Creates a new exception based on {@code cause}.
*
Expand All @@ -71,6 +95,8 @@ public static SpannerException newSpannerException(@Nullable Context context, Th
return newSpannerExceptionPreformatted(e.getErrorCode(), e.getMessage(), e);
} else if (cause instanceof CancellationException) {
return newSpannerExceptionForCancellation(context, cause);
} else if (cause instanceof ApiException) {
return fromApiException((ApiException) cause);
}
// Extract gRPC status. This will produce "UNKNOWN" for non-gRPC exceptions.
Status status = Status.fromThrowable(cause);
Expand Down Expand Up @@ -120,6 +146,17 @@ private static SpannerException newSpannerExceptionPreformatted(
}
}

private static SpannerException fromApiException(ApiException exception) {
Status.Code code = ((GrpcStatusCode) exception.getStatusCode()).getTransportCode();
ErrorCode errorCode = ErrorCode.fromGrpcStatus(Status.fromCode(code));
if (exception.getCause() != null) {
return SpannerExceptionFactory.newSpannerException(
errorCode, exception.getMessage(), exception.getCause());
} else {
return SpannerExceptionFactory.newSpannerException(errorCode, exception.getMessage());
}
}

private static boolean isRetryable(ErrorCode code, @Nullable Throwable cause) {
switch (code) {
case INTERNAL:
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.