Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,73 @@
/*
* 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.cloud.compute.v1.stub;

import com.google.api.core.BetaApi;
import com.google.api.gax.httpjson.HttpJsonLroErrorParser;
import com.google.api.gax.rpc.ErrorDetails;
import com.google.cloud.compute.v1.Errors;
import com.google.cloud.compute.v1.Operation;
import com.google.protobuf.Any;
import com.google.rpc.ErrorInfo;
import java.util.ArrayList;
import java.util.List;

@BetaApi("The surface for custom LRO error parsing is not stable yet and may change.")
class ComputeLroErrorParser implements HttpJsonLroErrorParser {

@Override
public ErrorDetails parse(Object response) {
if (!(response instanceof Operation)) {
return null;
}
Operation operation = ((Operation) response);
if (!operation.hasError()) {
return null;
}
List<Any> rawErrorMessages = new ArrayList<>();
for (Errors error : operation.getError().getErrorsList()) {
ErrorInfo errorInfo =
ErrorInfo.newBuilder()
.setReason(error.getCode())
.setDomain("googleapis.com")
.putMetadata("message", error.getMessage())
.putMetadata("location", error.getLocation())
.build();
rawErrorMessages.add(Any.pack(errorInfo));
}
Comment on lines +41 to +51

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Avoid adding empty strings to the ErrorInfo metadata map if message or location are not populated. We can conditionally add them only when they are non-empty to keep the metadata clean.

    List<Any> rawErrorMessages = new ArrayList<>();
    for (Errors error : operation.getError().getErrorsList()) {
      ErrorInfo.Builder errorInfoBuilder =
          ErrorInfo.newBuilder()
              .setReason(error.getCode())
              .setDomain("googleapis.com");
      if (!error.getMessage().isEmpty()) {
        errorInfoBuilder.putMetadata("message", error.getMessage());
      }
      if (!error.getLocation().isEmpty()) {
        errorInfoBuilder.putMetadata("location", error.getLocation());
      }
      rawErrorMessages.add(Any.pack(errorInfoBuilder.build()));
    }

return ErrorDetails.builder().setRawErrorMessages(rawErrorMessages).build();
}

@Override
public String parseErrorMessage(Object response) {
if (!(response instanceof Operation)) {
return null;
}
Operation operation = ((Operation) response);
if (!operation.hasError() || operation.getError().getErrorsCount() == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
for (Errors error : operation.getError().getErrorsList()) {
if (sb.length() > 0) {
sb.append("; ");
}
sb.append(error.getCode()).append(": ").append(error.getMessage());
}
return sb.toString();
Comment on lines +64 to +71

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If either code or message is empty, appending them directly with : results in poorly formatted error messages like : message or code: . It is better to conditionally format the string based on which fields are present.

    StringBuilder sb = new StringBuilder();
    for (Errors error : operation.getError().getErrorsList()) {
      String code = error.getCode();
      String message = error.getMessage();
      if (code.isEmpty() && message.isEmpty()) {
        continue;
      }
      if (sb.length() > 0) {
        sb.append("; ");
      }
      if (!code.isEmpty() && !message.isEmpty()) {
        sb.append(code).append(": ").append(message);
      } else if (!code.isEmpty()) {
        sb.append(code);
      } else {
        sb.append(message);
      }
    }
    return sb.toString();

}
}
1 change: 1 addition & 0 deletions librarian.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ libraries:
java:
omit_common_resources: true
keep:
- google-cloud-compute/src/main/java/com/google/cloud/compute/v1/stub/ComputeLroErrorParser.java
- google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/BaseTest.java
- google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITComputeGoldenSignals.java
- google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITPaginationTest.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.StringObjectValue;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.gapic.composer.store.TypeStore;
Expand Down Expand Up @@ -323,6 +325,26 @@ public static Expr createLroSettingsBuilderExpr(
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(t)
.build();

List<Expr> createArgs = new ArrayList<>();
createArgs.add(classFieldRefFn.apply(method.lro().responseType()));
if (service.pakkage().startsWith("com.google.cloud.compute.v1")
&& !service.pakkage().startsWith("com.google.cloud.compute.v1small")
&& operationResponseTransformer
.reference()
.pakkage()
.equals("com.google.api.gax.httpjson")) {
createArgs.add(
NewObjectExpr.builder()
.setType(
TypeNode.withReference(
VaporReference.builder()
.setName("ComputeLroErrorParser")
.setPakkage(service.pakkage() + ".stub")
.build()))
.build());
}

builderSettingsExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(builderSettingsExpr)
Expand All @@ -331,7 +353,7 @@ public static Expr createLroSettingsBuilderExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(operationResponseTransformer)
.setMethodName("create")
.setArguments(classFieldRefFn.apply(method.lro().responseType()))
.setArguments(createArgs)
.build())
.build();
builderSettingsExpr =
Expand Down
Loading