Navigation Menu

Skip to content
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

Spanner: Provide a way to get the gRPC status code from a SpannerException #14

Closed
sebright opened this issue Dec 19, 2019 · 4 comments · Fixed by #25
Closed

Spanner: Provide a way to get the gRPC status code from a SpannerException #14

sebright opened this issue Dec 19, 2019 · 4 comments · Fixed by #25
Assignees
Labels
api: spanner Issues related to the googleapis/java-spanner API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@sebright
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I wanted to convert exceptions thrown by google-cloud-spanner to instances of io.grpc.Status. For example, I wanted to set the status on OpenCensus tracing spans, and OpenCensus provides a utility that takes a gRPC Status.

I wasn't able to find an easy way to get the io.grpc.Status.Code from a SpannerException. However, I noticed that SpannerException has a getErrorCode() method that returns a com.google.cloud.spanner.ErrorCode, and ErrorCode has a field of type io.grpc.Status.Code:
https://github.com/googleapis/google-cloud-java/blob/b6a364a101560f0eb7499176649b53f3cab08854/google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/ErrorCode.java#L58

Describe the solution you'd like

I'm not sure what is the best solution. I assume that SpannerException.getErrorCode() doesn't return a io.grpc.Status.Code because io.grpc.Status.Code includes OK and an exception is always an error. One solution is to make the existing ErrorCode.getGrpcStatus()
method public.

Describe alternatives you've considered
I am currently working around this issue by parsing the enum name, though it is fragile:

Status.Code.valueOf(((SpannerException) e).getErrorCode().toString())

Additional context
Add any other context or screenshots about the feature request here.

@sebright
Copy link
Contributor Author

/cc @olavloite @skuruppu

@olavloite
Copy link
Collaborator

olavloite commented Dec 20, 2019

The error code used by SpannerException always corresponds to the status code, so what you can do is the following:

SpannerException se = ...;
Status status = Status.fromCodeValue(se.getCode());

@sebright Does that work for you?

@chingor13 chingor13 transferred this issue from googleapis/google-cloud-java Jan 7, 2020
@chingor13 chingor13 added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Jan 7, 2020
@olavloite
Copy link
Collaborator

@sebright Friendly ping.
Is this still needed, or is the above work-around good enough?

@sebright
Copy link
Contributor Author

sebright commented Jan 9, 2020

Sorry for the delay. I would rather not rely on getCode(), because it is defined in BaseServiceException (https://github.com/googleapis/java-core/blob/21d98a098a6d008085a770516d8a6f9010f3ecdd/google-cloud-core/src/main/java/com/google/cloud/BaseServiceException.java#L290-L291), so it's not specific to gRPC errors. I also couldn't find any documentation for the meaning of the integer code. It would help if BaseGrpcServiceException (superclass of SpannerException) overrode getCode() and documented that it returned the gRPC status code.

I would still prefer a method that returns a io.grpc.Status.Code, because it would be more type safe and harder to misinterpret the error space.

olavloite added a commit that referenced this issue Jan 9, 2020
@google-cloud-label-sync google-cloud-label-sync bot added the api: spanner Issues related to the googleapis/java-spanner API. label Jan 31, 2020
rajatbhatta pushed a commit to rajatbhatta/java-spanner that referenced this issue Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/java-spanner API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants