Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@

import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.ApiException;
import com.google.protobuf.Any;
import com.google.protobuf.Timestamp;
import com.google.rpc.Code;
import com.google.rpc.Status;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.PoetryError;
import com.google.showcase.v1beta1.WaitMetadata;
import com.google.showcase.v1beta1.WaitRequest;
import com.google.showcase.v1beta1.WaitResponse;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.threeten.bp.Duration;
Expand Down Expand Up @@ -193,4 +199,35 @@ void testHttpJson_LROUnsuccessfulResponse_exceedsTotalTimeout_throwsDeadlineExce
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}
}

@Test
void testGRPC_LROErrorResponse_propagatesErrorDetails() throws Exception {
EchoClient grpcClient = TestClientInitializer.createGrpcEchoClient();
try {
PoetryError poetryError =
PoetryError.newBuilder().setPoem("Roses are red, violets are blue").build();
Status status =
Status.newBuilder()
.setCode(Code.ALREADY_EXISTS_VALUE)
.setMessage("The resource already exists")
.addDetails(Any.pack(poetryError))
.build();
WaitRequest waitRequest = WaitRequest.newBuilder().setError(status).build();
OperationFuture<WaitResponse, WaitMetadata> operationFuture =
grpcClient.waitOperationCallable().futureCall(waitRequest);
ExecutionException exception = assertThrows(ExecutionException.class, operationFuture::get);
assertThat(exception.getCause()).isInstanceOf(ApiException.class);
ApiException apiException = (ApiException) exception.getCause();

// Verify that error details are successfully propagated
assertThat(apiException.getErrorDetails()).isNotNull();
PoetryError unpackedError = apiException.getErrorDetails().getMessage(PoetryError.class);
assertThat(unpackedError).isNotNull();
assertThat(unpackedError.getPoem()).isEqualTo("Roses are red, violets are blue");
} finally {
grpcClient.close();
grpcClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
package com.google.api.gax.grpc;

import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.rpc.ErrorDetails;
import com.google.api.gax.rpc.StatusCode;
import com.google.longrunning.Operation;
import io.grpc.Status;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* Implementation of OperationSnapshot based on gRPC.
Expand Down Expand Up @@ -79,6 +81,16 @@ public String getErrorMessage() {
return operation.getError().getMessage();
}

@Override
public @Nullable ErrorDetails getErrorDetails() {
if (operation.hasError() && operation.getError().getDetailsCount() > 0) {
return ErrorDetails.builder()
.setRawErrorMessages(operation.getError().getDetailsList())
.build();
}
return null;
}
Comment thread
nnicolee marked this conversation as resolved.

public static GrpcOperationSnapshot create(Operation operation) {
return new GrpcOperationSnapshot(operation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public ResponseT apply(OperationSnapshot operationSnapshot) {
+ operationSnapshot.getErrorMessage(),
null,
operationSnapshot.getErrorCode(),
false);
false,
operationSnapshot.getErrorDetails());
}

if (!(operationSnapshot.getResponse() instanceof Any)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,25 @@ void testAnyMetadataTransformer_mismatchedTypes() {
assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
}

@Test
void testAnyResponseTransformer_exceptionWithErrorDetails() {
ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
Color poetryError =
Color.newBuilder().setRed(1.0f).build(); // Use Color as a mock details payload
Status status =
Status.newBuilder()
.setCode(Code.UNAVAILABLE.value())
.addDetails(Any.pack(poetryError))
.build();
OperationSnapshot operationSnapshot =
GrpcOperationSnapshot.create(
Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());

UnavailableException exception =
assertThrows(UnavailableException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception.getErrorDetails()).isNotNull();
Truth.assertThat(exception.getErrorDetails().getMessage(Color.class)).isEqualTo(poetryError);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@

import com.google.api.core.InternalApi;
import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.rpc.ErrorDetails;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.longrunning.Operation;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* Implementation of OperationSnapshot based on REST transport.
Expand All @@ -50,20 +52,23 @@ public class HttpJsonOperationSnapshot implements OperationSnapshot {
private final Object response;
private final StatusCode errorCode;
private final String errorMessage;
private final @Nullable ErrorDetails errorDetails;

private HttpJsonOperationSnapshot(
String name,
Object metadata,
boolean done,
Object response,
StatusCode errorCode,
String errorMessage) {
String errorMessage,
@Nullable ErrorDetails errorDetails) {
this.name = name;
this.metadata = metadata;
this.done = done;
this.response = response;
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.errorDetails = errorDetails;
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -102,6 +107,11 @@ public String getErrorMessage() {
return this.errorMessage;
}

@Override
public @Nullable ErrorDetails getErrorDetails() {
return this.errorDetails;
}

public static HttpJsonOperationSnapshot create(Operation operation) {
return newBuilder().setOperation(operation).build();
}
Expand All @@ -117,6 +127,18 @@ public static class Builder {
private Object response;
private StatusCode errorCode;
private String errorMessage;
private @Nullable ErrorDetails errorDetails;

/**
* Sets the LRO error details.
*
* @param errorDetails the LRO error details
* @return the builder instance
*/
public Builder setErrorDetails(final @Nullable ErrorDetails errorDetails) {
this.errorDetails = errorDetails;
return this;
}

public Builder setName(String name) {
this.name = name;
Expand Down Expand Up @@ -153,11 +175,20 @@ private Builder setOperation(Operation operation) {
this.errorCode =
HttpJsonStatusCode.of(com.google.rpc.Code.forNumber(operation.getError().getCode()));
this.errorMessage = operation.getError().getMessage();
if (operation.hasError() && operation.getError().getDetailsCount() > 0) {
this.errorDetails =
ErrorDetails.builder()
.setRawErrorMessages(operation.getError().getDetailsList())
.build();
} else {
this.errorDetails = null;
}
Comment thread
nnicolee marked this conversation as resolved.
return this;
}

public HttpJsonOperationSnapshot build() {
return new HttpJsonOperationSnapshot(name, metadata, done, response, errorCode, errorMessage);
return new HttpJsonOperationSnapshot(
name, metadata, done, response, errorCode, errorMessage, errorDetails);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public ResponseT apply(OperationSnapshot operationSnapshot) {
+ operationSnapshot.getErrorMessage(),
null,
operationSnapshot.getErrorCode(),
false);
false,
operationSnapshot.getErrorDetails());
}

if (!(operationSnapshot.getResponse() instanceof Any)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.api.gax.rpc.ErrorDetails;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.protobuf.Any;
import com.google.protobuf.Empty;
import java.util.ArrayList;
import java.util.Collections;
import org.junit.jupiter.api.Test;

class HttpJsonOperationSnapshotTest {
Expand Down Expand Up @@ -86,4 +90,22 @@ void newBuilderTestNotDone() {
assertEquals(HttpJsonStatusCode.of(Code.OK), testOperationSnapshot.getErrorCode());
assertFalse(testOperationSnapshot.isDone());
}

@Test
void newBuilderTestWithErrorDetails() {
ErrorDetails errorDetails =
ErrorDetails.builder()
.setRawErrorMessages(Collections.singletonList(Any.pack(Empty.getDefaultInstance())))
.build();
HttpJsonOperationSnapshot testOperationSnapshot =
HttpJsonOperationSnapshot.newBuilder()
.setName("snapshot-details")
.setMetadata("Dallas")
.setDone(true)
.setError(400, "Bad Request")
.setErrorDetails(errorDetails)
.build();

assertEquals(errorDetails, testOperationSnapshot.getErrorDetails());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,25 @@ void testAnyMetadataTransformer_mismatchedTypes() {
assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
}

@Test
void testAnyResponseTransformer_exceptionWithErrorDetails() {
ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
Color poetryError =
Color.newBuilder().setRed(1.0f).build(); // Use Color as a mock details payload
Status status =
Status.newBuilder()
.setCode(Code.UNAVAILABLE.getNumber())
.addDetails(Any.pack(poetryError))
.build();
OperationSnapshot operationSnapshot =
HttpJsonOperationSnapshot.create(
Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());

UnavailableException exception =
assertThrows(UnavailableException.class, () -> transformer.apply(operationSnapshot));
Truth.assertThat(exception.getErrorDetails()).isNotNull();
Truth.assertThat(exception.getErrorDetails().getMessage(Color.class)).isEqualTo(poetryError);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
*/
package com.google.api.gax.longrunning;

import com.google.api.gax.rpc.ErrorDetails;
import com.google.api.gax.rpc.StatusCode;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* A snapshot of a long-running operation.
Expand Down Expand Up @@ -67,4 +69,14 @@ public interface OperationSnapshot {
* or if it succeeded, returns null.
*/
String getErrorMessage();

/**
* If the operation is done and it failed, returns the ErrorDetails; if the operation is not done
* or if it succeeded, returns null.
*
* @return the error details if the operation failed, null otherwise
*/
default @Nullable ErrorDetails getErrorDetails() {
return null;
}
}
Loading