-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(showcase): Add gRPC and HttpJson Showcase ITs to verify Error Details #13928
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
Open
nnicolee
wants to merge
11
commits into
main
Choose a base branch
from
test/showcase-errordetails
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+261
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7af718
test(showcase): add integration tests for error details
nnicolee 3d2c286
test(showcase): simplify EchoResponse class reference using import
nnicolee dbe16db
test(showcase): address code review feedback using custom TypeRegistr…
nnicolee f7a9e69
Merge branch 'main' into test/showcase-errordetails
nnicolee 0841a7b
Merge branch 'main' of github.com:googleapis/google-cloud-java into t…
nnicolee 3f6ce5a
test(showcase): address code review feedback on comments and unit tes…
nnicolee 57ddda5
Merge branch 'test/showcase-errordetails' of github.com:googleapis/go…
nnicolee 7b38f8c
added a note clarifying that there's no custom error types
nnicolee b13db45
Merge branch 'main' into test/showcase-errordetails
nnicolee f17cd3c
Merge branch 'main' into test/showcase-errordetails
nnicolee 5d75a81
added comments and httpjson variant and simplify customMessage assert…
nnicolee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
261 changes: 261 additions & 0 deletions
261
...-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITErrorDetails.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.showcase.v1beta1.it; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import com.google.api.client.http.HttpResponseException; | ||
| import com.google.api.gax.rpc.ApiException; | ||
| import com.google.api.gax.rpc.ErrorDetails; | ||
| import com.google.api.gax.rpc.StatusCode; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParser; | ||
| import com.google.protobuf.TypeRegistry; | ||
| import com.google.protobuf.util.JsonFormat; | ||
| import com.google.rpc.BadRequest; | ||
| import com.google.rpc.DebugInfo; | ||
| import com.google.rpc.ErrorInfo; | ||
| import com.google.rpc.Help; | ||
| import com.google.rpc.LocalizedMessage; | ||
| import com.google.rpc.PreconditionFailure; | ||
| import com.google.rpc.QuotaFailure; | ||
| import com.google.rpc.RequestInfo; | ||
| import com.google.rpc.ResourceInfo; | ||
| import com.google.rpc.RetryInfo; | ||
| import com.google.rpc.Status; | ||
| import com.google.showcase.v1beta1.EchoClient; | ||
| import com.google.showcase.v1beta1.FailEchoWithDetailsRequest; | ||
| import com.google.showcase.v1beta1.PoetryError; | ||
| import com.google.showcase.v1beta1.it.util.TestClientInitializer; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Integration tests for verifying that client libraries correctly propagate and deserialize | ||
| * standard and custom error details from {@link ApiException} over gRPC and HTTP/JSON transports. | ||
| */ | ||
| class ITErrorDetails { | ||
|
|
||
| private static final TypeRegistry SHOWCASE_ERROR_TYPE_REGISTRY = | ||
| TypeRegistry.newBuilder() | ||
| .add(ErrorInfo.getDescriptor()) | ||
| .add(RetryInfo.getDescriptor()) | ||
| .add(DebugInfo.getDescriptor()) | ||
| .add(QuotaFailure.getDescriptor()) | ||
| .add(PreconditionFailure.getDescriptor()) | ||
| .add(BadRequest.getDescriptor()) | ||
| .add(RequestInfo.getDescriptor()) | ||
| .add(ResourceInfo.getDescriptor()) | ||
| .add(Help.getDescriptor()) | ||
| .add(LocalizedMessage.getDescriptor()) | ||
| .add(PoetryError.getDescriptor()) | ||
| .build(); | ||
|
|
||
| private static EchoClient grpcClient; | ||
| private static EchoClient httpjsonClient; | ||
|
|
||
| @BeforeAll | ||
| static void createClients() throws Exception { | ||
| grpcClient = TestClientInitializer.createGrpcEchoClient(); | ||
| httpjsonClient = TestClientInitializer.createHttpJsonEchoClient(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void destroyClients() throws InterruptedException { | ||
| grpcClient.close(); | ||
| httpjsonClient.close(); | ||
|
|
||
| grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); | ||
| httpjsonClient.awaitTermination( | ||
| TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); | ||
| } | ||
|
nnicolee marked this conversation as resolved.
nnicolee marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Helper method to verify the content of error details. Transport-neutral validation of standard | ||
| * and custom error packets. | ||
| */ | ||
| private void verifyErrorDetailsContent(ErrorDetails errorDetails, String expectedPoem) { | ||
| // Verify standard error details are present and populated | ||
| // We are assuming that the mock server's hardcoded return values will stay the same | ||
| // https://github.com/googleapis/gapic-showcase/blob/b6c247f153369044d599969f6929ecdeb066c4c6/server/services/echo_service.go | ||
| ErrorInfo errorInfo = errorDetails.getErrorInfo(); | ||
| assertThat(errorInfo).isNotNull(); | ||
| assertThat(errorInfo.getReason()).isEqualTo("some ErrorInfo reason"); | ||
|
|
||
| RetryInfo retryInfo = errorDetails.getRetryInfo(); | ||
| assertThat(retryInfo).isNotNull(); | ||
| assertThat(retryInfo.getRetryDelay().getSeconds()).isEqualTo(11); | ||
|
|
||
| DebugInfo debugInfo = errorDetails.getDebugInfo(); | ||
| assertThat(debugInfo).isNotNull(); | ||
| assertThat(debugInfo.getDetail()).isEqualTo("a DebugInfo detail"); | ||
|
|
||
| QuotaFailure quotaFailure = errorDetails.getQuotaFailure(); | ||
| assertThat(quotaFailure).isNotNull(); | ||
| assertThat(quotaFailure.getViolations(0).getDescription()) | ||
| .isEqualTo("First QuotaFailure description"); | ||
|
|
||
| PreconditionFailure preconditionFailure = errorDetails.getPreconditionFailure(); | ||
| assertThat(preconditionFailure).isNotNull(); | ||
| assertThat(preconditionFailure.getViolations(0).getDescription()) | ||
| .isEqualTo("First PreconditionFailure description"); | ||
|
|
||
| BadRequest badRequest = errorDetails.getBadRequest(); | ||
| assertThat(badRequest).isNotNull(); | ||
| assertThat(badRequest.getFieldViolations(0).getDescription()) | ||
| .isEqualTo("First BadRequest description"); | ||
|
|
||
| RequestInfo requestInfo = errorDetails.getRequestInfo(); | ||
| assertThat(requestInfo).isNotNull(); | ||
| assertThat(requestInfo.getRequestId()).isEqualTo("RequestInfo: showcase-request-id"); | ||
| assertThat(requestInfo.getServingData()).isEqualTo("RequestInfo: showcase serving data"); | ||
|
|
||
| ResourceInfo resourceInfo = errorDetails.getResourceInfo(); | ||
| assertThat(resourceInfo).isNotNull(); | ||
| assertThat(resourceInfo.getResourceType()).isEqualTo("ResourceInfo: showcase resource"); | ||
|
|
||
| Help help = errorDetails.getHelp(); | ||
| assertThat(help).isNotNull(); | ||
| assertThat(help.getLinks(0).getDescription()).isEqualTo("Help: first showcase help link"); | ||
|
|
||
| LocalizedMessage localizedMessage = errorDetails.getLocalizedMessage(); | ||
| assertThat(localizedMessage).isNotNull(); | ||
| assertThat(localizedMessage.getLocale()).isEqualTo("fr-CH"); | ||
| assertThat(localizedMessage.getMessage()) | ||
| .isEqualTo("This LocalizedMessage should be treated specially"); | ||
|
|
||
| // Verify custom PoetryError can be unpacked and matches expected poem | ||
| PoetryError poetryError = errorDetails.getMessage(PoetryError.class); | ||
| assertThat(poetryError).isNotNull(); | ||
| assertThat(poetryError.getPoem()).isEqualTo(expectedPoem); | ||
| } | ||
|
|
||
| // Verifies error details are correctly propagated and unpacked over standard gRPC protocol | ||
| @Test | ||
| void testGrpc_failEchoWithDetails() { | ||
| FailEchoWithDetailsRequest request = FailEchoWithDetailsRequest.newBuilder().build(); | ||
| ApiException exception = | ||
| assertThrows(ApiException.class, () -> grpcClient.failEchoWithDetails(request)); | ||
|
|
||
| assertThat(exception.getStatusCode().getCode()).isEqualTo(StatusCode.Code.ABORTED); | ||
| assertThat(exception.getErrorDetails()).isNotNull(); | ||
|
|
||
| // Reuse Transport-neutral Validation | ||
| verifyErrorDetailsContent(exception.getErrorDetails(), "roses are red"); | ||
| } | ||
|
|
||
| // Verifies custom Error Details messages reflect user-defined inputs via gRPC | ||
| @Test | ||
| void testGrpc_failEchoWithDetails_customMessage() { | ||
| String customMessage = "this is a custom message to echo back"; | ||
| FailEchoWithDetailsRequest request = | ||
| FailEchoWithDetailsRequest.newBuilder().setMessage(customMessage).build(); | ||
| ApiException exception = | ||
| assertThrows(ApiException.class, () -> grpcClient.failEchoWithDetails(request)); | ||
|
|
||
| assertThat(exception.getStatusCode().getCode()).isEqualTo(StatusCode.Code.ABORTED); | ||
| assertThat(exception.getErrorDetails()).isNotNull(); | ||
|
|
||
| PoetryError poetryError = exception.getErrorDetails().getMessage(PoetryError.class); | ||
| assertThat(poetryError).isNotNull(); | ||
| assertThat(poetryError.getPoem()).isEqualTo(customMessage); | ||
| } | ||
|
|
||
| // Verifies error details are accessible in raw form over REST/HTTP and validates manually-parsed | ||
| // decompression | ||
| @Test | ||
| void testHttpJson_failEchoWithDetails() throws Exception { | ||
| FailEchoWithDetailsRequest request = FailEchoWithDetailsRequest.newBuilder().build(); | ||
| ApiException exception = | ||
| assertThrows(ApiException.class, () -> httpjsonClient.failEchoWithDetails(request)); | ||
| assertThat(exception.getStatusCode().getCode()).isEqualTo(StatusCode.Code.ABORTED); | ||
|
|
||
| // GAX HTTP/JSON parser limitation: Because the response contains a custom/unregistered type | ||
| // (PoetryError) in the Any details list, HttpJsonErrorParser fails to parse the status payload, | ||
| // resulting in empty ErrorDetails (where getErrorInfo() returns null). | ||
|
nnicolee marked this conversation as resolved.
|
||
| // Note: Standard Google Cloud services follow AIP-193 error details (ErrorInfo, RetryInfo, | ||
| // etc.) | ||
| // and do not use custom error payload types like PoetryError. | ||
| ErrorDetails errorDetails = exception.getErrorDetails(); | ||
| if (errorDetails != null) { | ||
| assertThat(errorDetails.getErrorInfo()).isNull(); | ||
| } | ||
|
nnicolee marked this conversation as resolved.
|
||
|
|
||
| // Workaround REST limitation: Parse the raw HTTP JSON error response manually using a custom | ||
| // TypeRegistry that registers standard types plus the showcase-specific PoetryError type. | ||
|
nnicolee marked this conversation as resolved.
|
||
| // This mimics the behavior of HttpJsonErrorParser, which currently does not support passing in | ||
| // a custom TypeRegistry. | ||
| assertThat(exception.getCause()).isInstanceOf(HttpResponseException.class); | ||
| HttpResponseException httpException = (HttpResponseException) exception.getCause(); | ||
| String errorJson = httpException.getContent(); | ||
| assertThat(errorJson).isNotNull(); | ||
|
|
||
| JsonFormat.Parser jsonParser = | ||
| JsonFormat.parser().ignoringUnknownFields().usingTypeRegistry(SHOWCASE_ERROR_TYPE_REGISTRY); | ||
|
|
||
| // Parse the raw HTTP error JSON payload formatted per AIP-193 HTTP/1.1 representation | ||
| // (rest_error.proto), | ||
| // where fields are wrapped under the top-level "error" JSON key. | ||
| JsonObject root = JsonParser.parseString(errorJson).getAsJsonObject(); | ||
| JsonObject errorObj = root.getAsJsonObject("error"); | ||
| Status.Builder statusBuilder = Status.newBuilder(); | ||
|
lqiu96 marked this conversation as resolved.
|
||
| jsonParser.merge(errorObj.toString(), statusBuilder); | ||
| Status status = statusBuilder.build(); | ||
|
|
||
| // Verify we can successfully unpack the details from our custom status instance | ||
| ErrorDetails parsedDetails = | ||
| ErrorDetails.builder().setRawErrorMessages(status.getDetailsList()).build(); | ||
|
|
||
| // Reuse Transport-neutral Validation! | ||
| verifyErrorDetailsContent(parsedDetails, "roses are red"); | ||
| } | ||
|
|
||
| // Verifies custom Error Details messages reflect user-defined inputs via HTTP/JSON | ||
| @Test | ||
| void testHttpJson_failEchoWithDetails_customMessage() throws Exception { | ||
| String customMessage = "this is a custom message to echo back"; | ||
| FailEchoWithDetailsRequest request = | ||
| FailEchoWithDetailsRequest.newBuilder().setMessage(customMessage).build(); | ||
| ApiException exception = | ||
| assertThrows(ApiException.class, () -> httpjsonClient.failEchoWithDetails(request)); | ||
|
|
||
| assertThat(exception.getStatusCode().getCode()).isEqualTo(StatusCode.Code.ABORTED); | ||
| assertThat(exception.getCause()).isInstanceOf(HttpResponseException.class); | ||
|
|
||
| HttpResponseException httpException = (HttpResponseException) exception.getCause(); | ||
| String errorJson = httpException.getContent(); | ||
| assertThat(errorJson).isNotNull(); | ||
|
|
||
| JsonFormat.Parser jsonParser = | ||
| JsonFormat.parser().ignoringUnknownFields().usingTypeRegistry(SHOWCASE_ERROR_TYPE_REGISTRY); | ||
|
|
||
| JsonObject root = JsonParser.parseString(errorJson).getAsJsonObject(); | ||
| JsonObject errorObj = root.getAsJsonObject("error"); | ||
| Status.Builder statusBuilder = Status.newBuilder(); | ||
| jsonParser.merge(errorObj.toString(), statusBuilder); | ||
| Status status = statusBuilder.build(); | ||
|
|
||
| ErrorDetails parsedDetails = | ||
| ErrorDetails.builder().setRawErrorMessages(status.getDetailsList()).build(); | ||
| PoetryError poetryError = parsedDetails.getMessage(PoetryError.class); | ||
| assertThat(poetryError).isNotNull(); | ||
| assertThat(poetryError.getPoem()).isEqualTo(customMessage); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.