|
16 | 16 |
|
17 | 17 | package com.google.cloud.storage;
|
18 | 18 |
|
| 19 | +import static com.google.cloud.storage.TestUtils.assertAll; |
19 | 20 | import static com.google.common.truth.Truth.assertThat;
|
20 | 21 |
|
21 | 22 | import com.google.api.gax.grpc.GrpcStatusCode;
|
|
25 | 26 | import com.google.cloud.BaseServiceException;
|
26 | 27 | import com.google.common.collect.ImmutableList;
|
27 | 28 | import com.google.protobuf.Any;
|
| 29 | +import com.google.protobuf.TextFormat; |
| 30 | +import com.google.protobuf.TextFormat.Printer; |
| 31 | +import com.google.rpc.BadRequest; |
| 32 | +import com.google.rpc.BadRequest.FieldViolation; |
28 | 33 | import com.google.rpc.DebugInfo;
|
29 | 34 | import com.google.rpc.ErrorInfo;
|
| 35 | +import com.google.rpc.Help; |
| 36 | +import com.google.rpc.Help.Link; |
| 37 | +import com.google.rpc.LocalizedMessage; |
| 38 | +import com.google.rpc.PreconditionFailure; |
| 39 | +import com.google.rpc.QuotaFailure; |
30 | 40 | import io.grpc.Status;
|
31 | 41 | import io.grpc.Status.Code;
|
32 | 42 | import io.grpc.StatusRuntimeException;
|
| 43 | +import java.util.List; |
33 | 44 | import org.junit.Test;
|
34 | 45 |
|
35 | 46 | public final class StorageExceptionGrpcCompatibilityTest {
|
@@ -114,6 +125,88 @@ public void testCoalesce_UNAUTHENTICATED() {
|
114 | 125 | doTestCoalesce(401, Code.UNAUTHENTICATED);
|
115 | 126 | }
|
116 | 127 |
|
| 128 | + @Test |
| 129 | + public void apiExceptionErrorDetails() throws Exception { |
| 130 | + ErrorInfo errorInfo = |
| 131 | + ErrorInfo.newBuilder() |
| 132 | + .setReason("STACKOUT") |
| 133 | + .setDomain("spanner.googlepais.com") |
| 134 | + .putMetadata("availableRegions", "us-central1,us-east2") |
| 135 | + .build(); |
| 136 | + DebugInfo debugInfo = |
| 137 | + DebugInfo.newBuilder() |
| 138 | + .addStackEntries("HEAD") |
| 139 | + .addStackEntries("HEAD~1") |
| 140 | + .addStackEntries("HEAD~2") |
| 141 | + .addStackEntries("HEAD~3") |
| 142 | + .setDetail("some detail") |
| 143 | + .build(); |
| 144 | + QuotaFailure quotaFailure = |
| 145 | + QuotaFailure.newBuilder() |
| 146 | + .addViolations( |
| 147 | + QuotaFailure.Violation.newBuilder() |
| 148 | + .setSubject("clientip:127.0.3.3") |
| 149 | + .setDescription("Daily limit") |
| 150 | + .build()) |
| 151 | + .build(); |
| 152 | + PreconditionFailure preconditionFailure = |
| 153 | + PreconditionFailure.newBuilder() |
| 154 | + .addViolations( |
| 155 | + PreconditionFailure.Violation.newBuilder() |
| 156 | + .setType("TOS") |
| 157 | + .setSubject("google.com/cloud") |
| 158 | + .setDescription("Terms of service not accepted") |
| 159 | + .build()) |
| 160 | + .build(); |
| 161 | + BadRequest badRequest = |
| 162 | + BadRequest.newBuilder() |
| 163 | + .addFieldViolations( |
| 164 | + FieldViolation.newBuilder() |
| 165 | + .setField("email_addresses[3].type[2]") |
| 166 | + .setDescription("duplicate value 'WORK'") |
| 167 | + .setReason("INVALID_EMAIL_ADDRESS_TYPE") |
| 168 | + .setLocalizedMessage( |
| 169 | + LocalizedMessage.newBuilder() |
| 170 | + .setLocale("en-US") |
| 171 | + .setMessage("Invalid email type: duplicate value") |
| 172 | + .build()) |
| 173 | + .build()) |
| 174 | + .build(); |
| 175 | + Help help = |
| 176 | + Help.newBuilder() |
| 177 | + .addLinks( |
| 178 | + Link.newBuilder().setDescription("link1").setUrl("https://google.com").build()) |
| 179 | + .build(); |
| 180 | + List<Any> errors = |
| 181 | + ImmutableList.of( |
| 182 | + Any.pack(errorInfo), |
| 183 | + Any.pack(debugInfo), |
| 184 | + Any.pack(quotaFailure), |
| 185 | + Any.pack(preconditionFailure), |
| 186 | + Any.pack(badRequest), |
| 187 | + Any.pack(help)); |
| 188 | + ErrorDetails errorDetails = ErrorDetails.builder().setRawErrorMessages(errors).build(); |
| 189 | + ApiException ae = |
| 190 | + ApiExceptionFactory.createException( |
| 191 | + Code.OUT_OF_RANGE.toStatus().asRuntimeException(), |
| 192 | + GrpcStatusCode.of(Code.OUT_OF_RANGE), |
| 193 | + false, |
| 194 | + errorDetails); |
| 195 | + |
| 196 | + BaseServiceException se = StorageException.coalesce(ae); |
| 197 | + String message = se.getCause().getSuppressed()[0].getMessage(); |
| 198 | + Printer printer = TextFormat.printer(); |
| 199 | + assertAll( |
| 200 | + () -> assertThat(message).contains("ErrorDetails {"), |
| 201 | + () -> assertThat(message).contains(printer.shortDebugString(errorInfo)), |
| 202 | + () -> assertThat(message).contains(printer.shortDebugString(debugInfo)), |
| 203 | + () -> assertThat(message).contains(printer.shortDebugString(quotaFailure)), |
| 204 | + () -> assertThat(message).contains(printer.shortDebugString(preconditionFailure)), |
| 205 | + () -> assertThat(message).contains(printer.shortDebugString(badRequest)), |
| 206 | + () -> assertThat(message).contains(printer.shortDebugString(help)), |
| 207 | + () -> assertThat(message).contains("\t}")); |
| 208 | + } |
| 209 | + |
117 | 210 | private void doTestCoalesce(int expectedCode, Code code) {
|
118 | 211 | Status status = code.toStatus();
|
119 | 212 | GrpcStatusCode statusCode = GrpcStatusCode.of(code);
|
|
0 commit comments