This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Error Details Improvements - GRPC (#1634)
Fixes: #1635 Key design notes: 1. All `ApiException` created for GRPC clients will go through `GrpcApiExceptionFactory` and the cause of `ApiException` already has all the info we need, hence the majority of the logic is in `GrpcApiExceptionFactory`. 2. Promoted all the fields of `ErrorInfo` to top level of `ApiException`, accessible through getters directly. 3. Encapsulated all the raw error messages from server to `ErrorDetails`. The unpacked error messages are accessible through getters. 4. If error messages from server are corrupted and unable to unpack, a `ProtocolBufferParsingException` will be thrown.
- Loading branch information
Showing
27 changed files
with
906 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
139 changes: 139 additions & 0 deletions
139
gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcApiExceptionFactoryTest.java
This file contains 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,139 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.google.api.gax.grpc; | ||
|
||
import static com.google.api.gax.grpc.GrpcApiExceptionFactory.ERROR_DETAIL_KEY; | ||
|
||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.api.gax.rpc.ErrorDetails; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.truth.Truth; | ||
import com.google.protobuf.Any; | ||
import com.google.protobuf.Duration; | ||
import com.google.rpc.ErrorInfo; | ||
import com.google.rpc.RetryInfo; | ||
import com.google.rpc.Status; | ||
import io.grpc.Metadata; | ||
import io.grpc.StatusException; | ||
import io.grpc.StatusRuntimeException; | ||
import java.util.Collections; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class GrpcApiExceptionFactoryTest { | ||
|
||
private static final ErrorInfo ERROR_INFO = | ||
ErrorInfo.newBuilder() | ||
.setDomain("googleapis.com") | ||
.setReason("SERVICE_DISABLED") | ||
.putAllMetadata(Collections.emptyMap()) | ||
.build(); | ||
|
||
private static final RetryInfo RETRY_INFO = | ||
RetryInfo.newBuilder().setRetryDelay(Duration.newBuilder().setSeconds(213).build()).build(); | ||
|
||
private static final ImmutableList<Any> RAW_ERROR_MESSAGES = | ||
ImmutableList.of(Any.pack(ERROR_INFO), Any.pack(RETRY_INFO)); | ||
|
||
private static final ErrorDetails ERROR_DETAILS = | ||
ErrorDetails.builder().setRawErrorMessages(RAW_ERROR_MESSAGES).build(); | ||
|
||
private static final io.grpc.Status GRPC_STATUS = io.grpc.Status.CANCELLED; | ||
|
||
private GrpcApiExceptionFactory factory; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
factory = new GrpcApiExceptionFactory(Collections.emptySet()); | ||
} | ||
|
||
@Test | ||
public void create_shouldCreateApiExceptionWithErrorDetailsForStatusException() { | ||
Metadata trailers = new Metadata(); | ||
Status status = Status.newBuilder().addAllDetails(RAW_ERROR_MESSAGES).build(); | ||
trailers.put( | ||
Metadata.Key.of(ERROR_DETAIL_KEY, Metadata.BINARY_BYTE_MARSHALLER), status.toByteArray()); | ||
StatusException statusException = new StatusException(GRPC_STATUS, trailers); | ||
|
||
ApiException actual = factory.create(statusException); | ||
|
||
Truth.assertThat(actual.getErrorDetails()).isEqualTo(ERROR_DETAILS); | ||
} | ||
|
||
@Test | ||
public void create_shouldCreateApiExceptionWithErrorDetailsForStatusRuntimeException() { | ||
Metadata trailers = new Metadata(); | ||
Status status = Status.newBuilder().addAllDetails(RAW_ERROR_MESSAGES).build(); | ||
trailers.put( | ||
Metadata.Key.of(ERROR_DETAIL_KEY, Metadata.BINARY_BYTE_MARSHALLER), status.toByteArray()); | ||
StatusRuntimeException statusException = new StatusRuntimeException(GRPC_STATUS, trailers); | ||
|
||
ApiException actual = factory.create(statusException); | ||
|
||
Truth.assertThat(actual.getErrorDetails()).isEqualTo(ERROR_DETAILS); | ||
} | ||
|
||
@Test | ||
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataIsNull() { | ||
StatusRuntimeException statusException = new StatusRuntimeException(GRPC_STATUS, null); | ||
|
||
ApiException actual = factory.create(statusException); | ||
|
||
Truth.assertThat(actual.getErrorDetails()).isNull(); | ||
} | ||
|
||
@Test | ||
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataDoesNotHaveErrorDetails() { | ||
StatusRuntimeException statusException = | ||
new StatusRuntimeException(GRPC_STATUS, new Metadata()); | ||
|
||
ApiException actual = factory.create(statusException); | ||
|
||
Truth.assertThat(actual.getErrorDetails()).isNull(); | ||
} | ||
|
||
@Test | ||
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfStatusIsMalformed() { | ||
Metadata trailers = new Metadata(); | ||
Status status = Status.newBuilder().addDetails(Any.pack(ERROR_INFO)).build(); | ||
byte[] bytes = status.toByteArray(); | ||
// manually manipulate status bytes array | ||
bytes[0] = 123; | ||
trailers.put(Metadata.Key.of(ERROR_DETAIL_KEY, Metadata.BINARY_BYTE_MARSHALLER), bytes); | ||
StatusRuntimeException statusException = new StatusRuntimeException(GRPC_STATUS, trailers); | ||
|
||
ApiException actual = factory.create(statusException); | ||
|
||
Truth.assertThat(actual.getErrorDetails()).isNull(); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.