-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: register custom ComputeLroErrorParser for Compute LROs #14065
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
Draft
nnicolee
wants to merge
2
commits into
feat/lro-generic-error-propagation
Choose a base branch
from
feat/lro-compute-poc2
base: feat/lro-generic-error-propagation
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.
Draft
Changes from all commits
Commits
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
73 changes: 73 additions & 0 deletions
73
...e-cloud-compute/src/main/java/com/google/cloud/compute/v1/stub/ComputeLroErrorParser.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,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)); | ||
| } | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If either 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(); |
||
| } | ||
| } | ||
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
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
46 changes: 46 additions & 0 deletions
46
...x-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonLroErrorParser.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,46 @@ | ||
| /* | ||
| * Copyright 2026 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.httpjson; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import com.google.api.gax.rpc.ErrorDetails; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| @NullMarked | ||
| @BetaApi("The surface for custom LRO error parsing is not stable yet and may change.") | ||
| public interface HttpJsonLroErrorParser { | ||
| /** Parses custom LRO response object into standard ErrorDetails. */ | ||
| @Nullable ErrorDetails parse(Object response); | ||
|
|
||
| /** Concatenates custom LRO response errors into a single descriptive message. */ | ||
| @Nullable String parseErrorMessage(Object response); | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid adding empty strings to the
ErrorInfometadata map ifmessageorlocationare not populated. We can conditionally add them only when they are non-empty to keep the metadata clean.