feat: register custom ComputeLroErrorParser for Compute LROs - #14065
feat: register custom ComputeLroErrorParser for Compute LROs#14065nnicolee wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a custom LRO error parser (ComputeLroErrorParser) for the Google Cloud Compute Java client and updates the GAPIC generator (RetrySettingsComposer) to conditionally instantiate and pass this parser when generating LRO settings for com.google.cloud.compute.v1 services. The reviewer provided feedback on ComputeLroErrorParser to improve robustness: first, by conditionally adding message and location to the ErrorInfo metadata map only when they are non-empty; and second, by handling cases where code or message might be empty to prevent poorly formatted error messages (e.g., : message or code: ).
| 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)); | ||
| } |
There was a problem hiding this comment.
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()));
}| 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(); |
There was a problem hiding this comment.
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();
|
|


Overview
This is POC 2 that implements Phase 2 of Compute Engine's custom REST LRO error details propagation.
Instead of generating the parser class via code generation (which was the approach in POC 1), this POC introduces a manually written, package-private parser class in the Compute stub module and configures the code generator to register this parser during stub initialization. The parser class is protected from being wiped out during future Librarian code regeneration.
Key Changes
ComputeLroErrorParser.javainside thejava-computestub package. It extracts nested Compute LRO errors fromOperationpayloads and translates them into standardErrorDetails(utilizingErrorInfo).librarian.yamlto includeComputeLroErrorParser.javain thekeepblock. This prevents the Librarian regeneration tool from deleting or overwriting the manual parser file.RetrySettingsComposer.javato automatically generate the registration ofnew ComputeLroErrorParser()inside the generated Compute stub settings (likeAddressesStubSettings.java,NodeTemplatesStubSettings.java, etc.).com.google.cloud.compute.v1smallfrom parser registration insideRetrySettingsComposer.java. This keeps the integration test goldens untouched and ensures the test suite compiles and passes cleanly without requiring a mock parser.