|
25 | 25 | import com.google.api.gax.grpc.testing.LocalChannelProvider;
|
26 | 26 | import com.google.api.gax.retrying.RetrySettings;
|
27 | 27 | import com.google.cloud.NoCredentials;
|
| 28 | +import com.google.cloud.spanner.AbstractResultSet.GrpcStreamIterator; |
28 | 29 | import com.google.cloud.spanner.AsyncResultSet.CallbackResponse;
|
29 | 30 | import com.google.cloud.spanner.AsyncResultSet.ReadyCallback;
|
30 | 31 | import com.google.cloud.spanner.AsyncRunner.AsyncWork;
|
|
33 | 34 | import com.google.cloud.spanner.ReadContext.QueryAnalyzeMode;
|
34 | 35 | import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
|
35 | 36 | import com.google.common.base.Stopwatch;
|
| 37 | +import com.google.common.collect.ImmutableList; |
36 | 38 | import com.google.common.util.concurrent.SettableFuture;
|
37 | 39 | import com.google.protobuf.AbstractMessage;
|
38 | 40 | import com.google.spanner.v1.ExecuteSqlRequest;
|
|
52 | 54 | import java.util.concurrent.ScheduledThreadPoolExecutor;
|
53 | 55 | import java.util.concurrent.TimeUnit;
|
54 | 56 | import java.util.concurrent.atomic.AtomicInteger;
|
| 57 | +import java.util.logging.Level; |
| 58 | +import java.util.logging.Logger; |
55 | 59 | import org.junit.After;
|
56 | 60 | import org.junit.AfterClass;
|
57 | 61 | import org.junit.Before;
|
@@ -1483,4 +1487,65 @@ public void testBatchCreateSessionsPermissionDenied() {
|
1483 | 1487 | }
|
1484 | 1488 | }
|
1485 | 1489 | }
|
| 1490 | + |
| 1491 | + @Test |
| 1492 | + public void testExceptionIncludesStatement() { |
| 1493 | + mockSpanner.setExecuteStreamingSqlExecutionTime( |
| 1494 | + SimulatedExecutionTime.ofException( |
| 1495 | + Status.INVALID_ARGUMENT.withDescription("Invalid query").asRuntimeException())); |
| 1496 | + DatabaseClient client = |
| 1497 | + spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE)); |
| 1498 | + try (ResultSet rs = |
| 1499 | + client |
| 1500 | + .singleUse() |
| 1501 | + .executeQuery( |
| 1502 | + Statement.newBuilder("SELECT * FROM FOO WHERE ID=@id").bind("id").to(1L).build())) { |
| 1503 | + rs.next(); |
| 1504 | + fail("missing expected exception"); |
| 1505 | + } catch (SpannerException e) { |
| 1506 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT); |
| 1507 | + assertThat(e.getMessage()).contains("Statement: 'SELECT * FROM FOO WHERE ID=@id'"); |
| 1508 | + // The error message should normally not include the parameter values to prevent sensitive |
| 1509 | + // information from accidentally being logged. |
| 1510 | + assertThat(e.getMessage()).doesNotContain("id: 1"); |
| 1511 | + } |
| 1512 | + |
| 1513 | + mockSpanner.setExecuteStreamingSqlExecutionTime( |
| 1514 | + SimulatedExecutionTime.ofException( |
| 1515 | + Status.INVALID_ARGUMENT.withDescription("Invalid query").asRuntimeException())); |
| 1516 | + Logger logger = Logger.getLogger(GrpcStreamIterator.class.getName()); |
| 1517 | + Level currentLevel = logger.getLevel(); |
| 1518 | + try (ResultSet rs = |
| 1519 | + client |
| 1520 | + .singleUse() |
| 1521 | + .executeQuery( |
| 1522 | + Statement.newBuilder("SELECT * FROM FOO WHERE ID=@id").bind("id").to(1L).build())) { |
| 1523 | + logger.setLevel(Level.FINEST); |
| 1524 | + rs.next(); |
| 1525 | + fail("missing expected exception"); |
| 1526 | + } catch (SpannerException e) { |
| 1527 | + // With log level set to FINEST the error should also include the parameter values. |
| 1528 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT); |
| 1529 | + assertThat(e.getMessage()).contains("Statement: 'SELECT * FROM FOO WHERE ID=@id {id: 1}'"); |
| 1530 | + } finally { |
| 1531 | + logger.setLevel(currentLevel); |
| 1532 | + } |
| 1533 | + } |
| 1534 | + |
| 1535 | + @Test |
| 1536 | + public void testReadDoesNotIncludeStatement() { |
| 1537 | + mockSpanner.setStreamingReadExecutionTime( |
| 1538 | + SimulatedExecutionTime.ofException( |
| 1539 | + Status.INVALID_ARGUMENT.withDescription("Invalid read").asRuntimeException())); |
| 1540 | + DatabaseClient client = |
| 1541 | + spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE)); |
| 1542 | + try (ResultSet rs = |
| 1543 | + client.singleUse().read("FOO", KeySet.singleKey(Key.of(1L)), ImmutableList.of("BAR"))) { |
| 1544 | + rs.next(); |
| 1545 | + fail("missing expected exception"); |
| 1546 | + } catch (SpannerException e) { |
| 1547 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT); |
| 1548 | + assertThat(e.getMessage()).doesNotContain("Statement:"); |
| 1549 | + } |
| 1550 | + } |
1486 | 1551 | }
|
0 commit comments