Skip to content

Commit 54fa826

Browse files
committed
chore: fix formatting and tests
1 parent 6bedf84 commit 54fa826

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.cloud.spanner;
1818

1919
import static com.google.cloud.spanner.SpannerExceptionFactory.asSpannerException;
20-
import static com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException;
2120
import static com.google.common.base.Preconditions.checkState;
2221

2322
import com.google.api.core.InternalApi;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ public ErrorCode getErrorCode() {
114114
return code;
115115
}
116116

117-
/** Returns the PostgreSQL SQLState error code that is encoded in this exception, or null if this {@link SpannerException} does not include a PostgreSQL error code. */
117+
/**
118+
* Returns the PostgreSQL SQLState error code that is encoded in this exception, or null if this
119+
* {@link SpannerException} does not include a PostgreSQL error code.
120+
*/
118121
public String getPostgreSQLErrorCode() {
119122
ErrorDetails details = getErrorDetails();
120123
if (details == null || details.getErrorInfo() == null) {

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,10 @@ private static ResourceInfo extractResourceInfo(Throwable cause) {
297297
return null;
298298
}
299299

300-
private static ErrorInfo extractErrorInfo(Throwable cause) {
300+
private static ErrorInfo extractErrorInfo(Throwable cause, ApiException apiException) {
301+
if (apiException != null && apiException.getErrorDetails() != null) {
302+
return apiException.getErrorDetails().getErrorInfo();
303+
}
301304
if (cause != null) {
302305
Metadata trailers = Status.trailersFromThrowable(cause);
303306
if (trailers != null) {
@@ -307,7 +310,11 @@ private static ErrorInfo extractErrorInfo(Throwable cause) {
307310
return null;
308311
}
309312

310-
static ErrorDetails extractErrorDetails(Throwable cause) {
313+
static ErrorDetails extractErrorDetails(Throwable cause, ApiException apiException) {
314+
if (apiException != null && apiException.getErrorDetails() != null) {
315+
return apiException.getErrorDetails();
316+
}
317+
311318
Throwable prevCause = null;
312319
while (cause != null && cause != prevCause) {
313320
if (cause instanceof ApiException) {
@@ -356,7 +363,7 @@ static SpannerException newSpannerExceptionPreformatted(
356363
case ABORTED:
357364
return new AbortedException(token, message, cause, apiException, reqId);
358365
case RESOURCE_EXHAUSTED:
359-
ErrorInfo info = extractErrorInfo(cause);
366+
ErrorInfo info = extractErrorInfo(cause, apiException);
360367
if (info != null
361368
&& info.getMetadataMap()
362369
.containsKey(AdminRequestsPerMinuteExceededException.ADMIN_REQUESTS_LIMIT_KEY)
@@ -382,7 +389,7 @@ static SpannerException newSpannerExceptionPreformatted(
382389
}
383390
}
384391
case INVALID_ARGUMENT:
385-
if (isTransactionMutationLimitException(cause)) {
392+
if (isTransactionMutationLimitException(cause, apiException)) {
386393
return new TransactionMutationLimitExceededException(
387394
token, code, message, cause, apiException, reqId);
388395
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static boolean isTransactionMutationLimitException(ErrorCode code, String messag
4343
return code == ErrorCode.INVALID_ARGUMENT && message != null && message.contains(ERROR_MESSAGE);
4444
}
4545

46-
static boolean isTransactionMutationLimitException(Throwable cause) {
46+
static boolean isTransactionMutationLimitException(Throwable cause, ApiException apiException) {
4747
if (cause == null
4848
|| cause.getMessage() == null
4949
|| !cause.getMessage().contains(ERROR_MESSAGE)) {
@@ -53,7 +53,7 @@ static boolean isTransactionMutationLimitException(Throwable cause) {
5353
// was that the transaction mutation limit was exceeded. We use that here to identify the error,
5454
// as there is no other specific metadata in the error that identifies it (other than the error
5555
// message).
56-
ErrorDetails errorDetails = extractErrorDetails(cause);
56+
ErrorDetails errorDetails = extractErrorDetails(cause, apiException);
5757
if (errorDetails != null && errorDetails.getHelp() != null) {
5858
return errorDetails.getHelp().getLinksCount() == 1
5959
&& errorDetails

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITQueryTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,21 @@ public void simple() {
132132

133133
@Test
134134
public void badQuery() {
135-
SpannerException exception = assertThrows(SpannerException.class, () -> execute(Statement.of("SELECT Apples AND Oranges"), Type.int64()));
135+
SpannerException exception =
136+
assertThrows(
137+
SpannerException.class,
138+
() -> execute(Statement.of("SELECT Apples AND Oranges"), Type.int64()));
136139
assertEquals(ErrorCode.INVALID_ARGUMENT, exception.getErrorCode());
137140
if (dialect.dialect == Dialect.POSTGRESQL) {
138-
assertTrue(exception.getMessage(), exception.getMessage().contains("column \"apples\" does not exist"));
141+
assertTrue(
142+
exception.getMessage(),
143+
exception.getMessage().contains("column \"apples\" does not exist"));
139144
// See https://www.postgresql.org/docs/current/errcodes-appendix.html
140145
// '42703' == undefined_column
141146
assertEquals("42703", exception.getPostgreSQLErrorCode());
142147
} else {
143-
assertTrue(exception.getMessage(), exception.getMessage().contains("Unrecognized name: Apples"));
148+
assertTrue(
149+
exception.getMessage(), exception.getMessage().contains("Unrecognized name: Apples"));
144150
}
145151
}
146152

0 commit comments

Comments
 (0)