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
Comments
/cc @olavloite @skuruppu |
The error code used by SpannerException se = ...;
Status status = Status.fromCodeValue(se.getCode()); @sebright Does that work for you? |
@sebright Friendly ping. |
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. |
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 agetErrorCode()
method that returns acom.google.cloud.spanner.ErrorCode
, and ErrorCode has a field of typeio.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 aio.grpc.Status.Code
becauseio.grpc.Status.Code
includes OK and an exception is always an error. One solution is to make the existingErrorCode.getGrpcStatus()
method public.
Describe alternatives you've considered
I am currently working around this issue by parsing the enum name, though it is fragile:
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: