| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new utility, HttpJsonErrorParser, designed to standardize and simplify the parsing of Google Cloud error responses received over HTTP/JSON. This enhancement allows the GAX framework to more effectively extract structured error details from JSON payloads, improving error handling capabilities for clients interacting with Google Cloud APIs. Highlights
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here. Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Sorry, something went wrong.
There was a problem hiding this comment.
This pull request introduces a new HttpJsonErrorParser utility for parsing error details from JSON responses, along with corresponding unit tests. The implementation is sound, but I have one suggestion to improve both performance and correctness. The suggestion involves avoiding unnecessary object creation and ensuring the method's behavior aligns with the expectations set by the unit tests, specifically regarding the handling of payloads without error details.
Sorry, something went wrong.
| */ | ||
| public static ErrorDetails parseErrorDetails(String errorJson) { | ||
| if (errorJson == null || errorJson.isEmpty()) { | ||
| return null; |
There was a problem hiding this comment.
It's better to use an empty ErrorDetails object to avoid null checks by the caller.
Sorry, something went wrong.
There was a problem hiding this comment.
Makes sense! Done, and returned in case of problems reading the errorinfo too.
Sorry, something went wrong.
| private static final TypeRegistry ERROR_INFO_REGISTRY = | ||
| TypeRegistry.newBuilder().add(ErrorInfo.getDescriptor()).build(); | ||
| private static final JsonFormat.Parser JSON_PARSER = | ||
| JsonFormat.parser().ignoringUnknownFields().usingTypeRegistry(ERROR_INFO_REGISTRY); |
There was a problem hiding this comment.
This is probably OK for now since we only need getErrorInfo(). But the ErrorDetails we are building is going to error out if we try to call getDebugInfo().
Sorry, something went wrong.
There was a problem hiding this comment.
I pulled some more things into the registry, PTAL
Sorry, something went wrong.
| ErrorDetails.Builder errorDetailsBuilder = ErrorDetails.builder(); | ||
| errorDetailsBuilder.setRawErrorMessages(status.getDetailsList()); | ||
| return errorDetailsBuilder.build(); | ||
| } catch (InvalidProtocolBufferException e) { |
There was a problem hiding this comment.
Which method might throw InvalidProtocolBufferException?
Sorry, something went wrong.
There was a problem hiding this comment.
I wrapped it more closely, thanks!
Sorry, something went wrong.
| errorDetailsBuilder.setRawErrorMessages(status.getDetailsList()); | ||
| return errorDetailsBuilder.build(); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| LOGGER.log(Level.WARNING, "Failed to parse Google Cloud error response", e); |
There was a problem hiding this comment.
This and following catch block swallowed the exception, which I don't think is a best practice. It's better to re-throw them with a runtime exception and let the caller decide how to handle them.
Sorry, something went wrong.
There was a problem hiding this comment.
I changed this to just return the empty object. I think it is expected that some results will have a field called "error" that has something else in it.
Sorry, something went wrong.
|
|
||
| try { | ||
| JsonObject root = JsonParser.parseString(errorJson).getAsJsonObject(); | ||
| if (!root.has("error")) { |
There was a problem hiding this comment.
Is the format of the error specified somewhere? Could it be in fields other than "error"?
Sorry, something went wrong.
There was a problem hiding this comment.
added a link to https://google.aip.dev/193#http11json-representation to the class
Sorry, something went wrong.
…HttpJsonErrorParser
…d of throwing RuntimeException
…s and localize catches
…name method to parseStatus
There was a problem hiding this comment.
thanks for the detailed feedback, I think it looks much better.
I changed the return type to Status and made all the (expected for many APIs) failure paths return an empty status.
Sorry, something went wrong.
|
|
||
| try { | ||
| JsonObject root = JsonParser.parseString(errorJson).getAsJsonObject(); | ||
| if (!root.has("error")) { |
There was a problem hiding this comment.
added a link to https://google.aip.dev/193#http11json-representation to the class
Sorry, something went wrong.
| */ | ||
| public static ErrorDetails parseErrorDetails(String errorJson) { | ||
| if (errorJson == null || errorJson.isEmpty()) { | ||
| return null; |
There was a problem hiding this comment.
Makes sense! Done, and returned in case of problems reading the errorinfo too.
Sorry, something went wrong.
| private static final TypeRegistry ERROR_INFO_REGISTRY = | ||
| TypeRegistry.newBuilder().add(ErrorInfo.getDescriptor()).build(); | ||
| private static final JsonFormat.Parser JSON_PARSER = | ||
| JsonFormat.parser().ignoringUnknownFields().usingTypeRegistry(ERROR_INFO_REGISTRY); |
There was a problem hiding this comment.
I pulled some more things into the registry, PTAL
Sorry, something went wrong.
| ErrorDetails.Builder errorDetailsBuilder = ErrorDetails.builder(); | ||
| errorDetailsBuilder.setRawErrorMessages(status.getDetailsList()); | ||
| return errorDetailsBuilder.build(); | ||
| } catch (InvalidProtocolBufferException e) { |
There was a problem hiding this comment.
I wrapped it more closely, thanks!
Sorry, something went wrong.
| errorDetailsBuilder.setRawErrorMessages(status.getDetailsList()); | ||
| return errorDetailsBuilder.build(); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| LOGGER.log(Level.WARNING, "Failed to parse Google Cloud error response", e); |
There was a problem hiding this comment.
I changed this to just return the empty object. I think it is expected that some results will have a field called "error" that has something else in it.
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM, thanks!
Sorry, something went wrong.
🤖 I have created a release *beep* *boop* --- <details><summary>2.68.0</summary> ## [2.68.0](v2.67.0...v2.68.0) (2026-03-17) ### Features * Add client request duration metric. ([#4132](#4132)) ([6a76397](6a76397)) * Add more attributes to golden signals metrics. ([#4135](#4135)) ([59d0624](59d0624)) * **gax-httpjson:** add HttpJsonErrorParser utility ([#4137](#4137)) ([a1b7565](a1b7565)) * **generator:** add extra allowed modules that will not be removed from the monorepo if they are present ([#4124](#4124)) ([774fe6e](774fe6e)) * **o11y:** introduce `gcp.client.repo` and `gcp.client.artifact` attributes ([#4120](#4120)) ([105f644](105f644)) * **o11y:** Introduce `rpc.system.name` and `rpc.method` in gRPC ([#4121](#4121)) ([7ab6d2e](7ab6d2e)) * **o11y:** introduce server.port attribute ([#4128](#4128)) ([56aa343](56aa343)) ### Bug Fixes * add null checks for ApiTracerFactory in ClientContext ([#4122](#4122)) ([4b3dbe2](4b3dbe2)) * Decrease log level for directpath warnings outside GCE ([#4139](#4139)) ([c9651e7](c9651e7)) * **gax-grpc:** add pick_first fallback to direct path service config ([#4143](#4143)) ([b150fe9](b150fe9)) * Populate method level attributes in metrics recording ([#4149](#4149)) ([7b7e6c9](7b7e6c9)) * suppress warnings in generated projects for non-idiomatic durations ([#4119](#4119)) ([4206e6e](4206e6e)) * Use ServiceName + MethodName as the regex for Otel ([#2543](#2543)) ([b9ae73f](b9ae73f)) ### Documentation * **hermetic_build:** fix config field name in readme ([#4130](#4130)) ([a0c8f67](a0c8f67)) </details> --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
🤖 I have created a release *beep* *boop* --- <details><summary>2.68.0</summary> ## [2.68.0](googleapis/sdk-platform-java@v2.67.0...v2.68.0) (2026-03-17) ### Features * Add client request duration metric. ([#4132](googleapis/sdk-platform-java#4132)) ([487650e](googleapis/sdk-platform-java@487650e)) * Add more attributes to golden signals metrics. ([#4135](googleapis/sdk-platform-java#4135)) ([bc82dcb](googleapis/sdk-platform-java@bc82dcb)) * **gax-httpjson:** add HttpJsonErrorParser utility ([#4137](googleapis/sdk-platform-java#4137)) ([6fe2446](googleapis/sdk-platform-java@6fe2446)) * **generator:** add extra allowed modules that will not be removed from the monorepo if they are present ([#4124](googleapis/sdk-platform-java#4124)) ([6a440da](googleapis/sdk-platform-java@6a440da)) * **o11y:** introduce `gcp.client.repo` and `gcp.client.artifact` attributes ([#4120](googleapis/sdk-platform-java#4120)) ([4954de5](googleapis/sdk-platform-java@4954de5)) * **o11y:** Introduce `rpc.system.name` and `rpc.method` in gRPC ([#4121](googleapis/sdk-platform-java#4121)) ([3593c30](googleapis/sdk-platform-java@3593c30)) * **o11y:** introduce server.port attribute ([#4128](googleapis/sdk-platform-java#4128)) ([1b10e02](googleapis/sdk-platform-java@1b10e02)) ### Bug Fixes * add null checks for ApiTracerFactory in ClientContext ([#4122](googleapis/sdk-platform-java#4122)) ([ded1922](googleapis/sdk-platform-java@ded1922)) * Decrease log level for directpath warnings outside GCE ([#4139](googleapis/sdk-platform-java#4139)) ([5151f34](googleapis/sdk-platform-java@5151f34)) * **gax-grpc:** add pick_first fallback to direct path service config ([#4143](googleapis/sdk-platform-java#4143)) ([4934ad8](googleapis/sdk-platform-java@4934ad8)) * Populate method level attributes in metrics recording ([#4149](googleapis/sdk-platform-java#4149)) ([69aabf8](googleapis/sdk-platform-java@69aabf8)) * suppress warnings in generated projects for non-idiomatic durations ([#4119](googleapis/sdk-platform-java#4119)) ([8a0c565](googleapis/sdk-platform-java@8a0c565)) * Use ServiceName + MethodName as the regex for Otel ([#2543](googleapis/sdk-platform-java#2543)) ([8196b8f](googleapis/sdk-platform-java@8196b8f)) ### Documentation * **hermetic_build:** fix config field name in readme ([#4130](googleapis/sdk-platform-java#4130)) ([0d98c37](googleapis/sdk-platform-java@0d98c37)) </details> --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
| Back | FazBrowse Home | New Git URL |
This PR introduces the HttpJsonErrorParser, a shared utility within gax-httpjson responsible for extracting and unpacking com.google.rpc.Status payloads embedded within HTTP JSON error responses.
Specifically, it uses JsonFormat and a custom TypeRegistry to cleanly unpack Any details (such as google.rpc.ErrorInfo) into a standard com.google.api.gax.rpc.ErrorDetails object. This achieves structural parity with how errors are extracted from gRPC trailers, doing so without consuming the underlying InputStream on the wire.
Context & Future Use
This utility is the first building block in a broader initiative to implement production logging across the Java client libraries. In upcoming PRs, this parser will be utilized directly by the HttpJsonLoggingInterceptor to safely unpack error metadata (like reason, domain, and metadata from ErrorInfo).
Testing