feat(gax-httpjson): populate ErrorDetails in HttpJsonApiExceptionFactory#4145
feat(gax-httpjson): populate ErrorDetails in HttpJsonApiExceptionFactory#4145westarle wants to merge 2 commits intogoogleapis:mainfrom
Conversation
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 significantly improves the error reporting capabilities within the Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request enhances the HttpJsonApiExceptionFactory to parse and populate ErrorDetails from HTTP JSON error responses, providing more granular error information. The changes correctly extract the status code, message, and details from the response body, overriding the transport-level error information where appropriate. The addition of a comprehensive test suite in HttpJsonApiExceptionFactoryTest is excellent and covers various scenarios, ensuring the new logic is robust. My feedback focuses on a small refactoring to improve code clarity by avoiding parameter reassignment and removing a redundant check, which will enhance maintainability.
| if (status.getMessage() != null && !status.getMessage().isEmpty()) { | ||
| message = status.getMessage(); | ||
| throwable = | ||
| new HttpResponseException.Builder( | ||
| e.getStatusCode(), e.getStatusMessage(), e.getHeaders()) | ||
| .setContent(e.getContent()) | ||
| .setMessage(message) | ||
| .build(); | ||
| } | ||
|
|
||
| if (status.getDetailsCount() > 0) { | ||
| ErrorDetails errorDetails = | ||
| ErrorDetails.builder().setRawErrorMessages(status.getDetailsList()).build(); | ||
| return ApiExceptionFactory.createException(throwable, statusCode, canRetry, errorDetails); | ||
| } | ||
| return createApiException(throwable, statusCode, message, canRetry); |
There was a problem hiding this comment.
For improved clarity and to avoid reassigning a method parameter, I suggest introducing a new cause variable to hold the exception that will be wrapped by the ApiException. This avoids modifying the throwable parameter, which can be confusing.
Additionally, status.getMessage() on a protobuf-generated class will return an empty string if not set, not null. The null check is redundant and can be removed.
| if (status.getMessage() != null && !status.getMessage().isEmpty()) { | |
| message = status.getMessage(); | |
| throwable = | |
| new HttpResponseException.Builder( | |
| e.getStatusCode(), e.getStatusMessage(), e.getHeaders()) | |
| .setContent(e.getContent()) | |
| .setMessage(message) | |
| .build(); | |
| } | |
| if (status.getDetailsCount() > 0) { | |
| ErrorDetails errorDetails = | |
| ErrorDetails.builder().setRawErrorMessages(status.getDetailsList()).build(); | |
| return ApiExceptionFactory.createException(throwable, statusCode, canRetry, errorDetails); | |
| } | |
| return createApiException(throwable, statusCode, message, canRetry); | |
| Throwable cause = throwable; | |
| if (!status.getMessage().isEmpty()) { | |
| message = status.getMessage(); | |
| cause = | |
| new HttpResponseException.Builder( | |
| e.getStatusCode(), e.getStatusMessage(), e.getHeaders()) | |
| .setContent(e.getContent()) | |
| .setMessage(message) | |
| .build(); | |
| } | |
| if (status.getDetailsCount() > 0) { | |
| ErrorDetails errorDetails = | |
| ErrorDetails.builder().setRawErrorMessages(status.getDetailsList()).build(); | |
| return ApiExceptionFactory.createException(cause, statusCode, canRetry, errorDetails); | |
| } | |
| return createApiException(cause, statusCode, message, canRetry); |
e522952 to
cc9a253
Compare
466bba4 to
90fc7af
Compare
90fc7af to
d03cdc3
Compare
| new HttpResponseException.Builder( | ||
| e.getStatusCode(), e.getStatusMessage(), e.getHeaders()) | ||
| .setContent(e.getContent()) | ||
| .setMessage(message) |
There was a problem hiding this comment.
This seems a bad practice to re-write an exception with a different message. Because when this exception shows up in the stacktrace, it looks as if the message is from the upstream library (http-client), but it is actually being overridden in downstream library (gax).
Is status.getMessage() required? What are the differences between status.getMessage() and the existing message? If it is indeed required, we can create ApiExceptionFactory.createException(message, cause, statusCode, canRetry, errorDetails) that wraps the message with a new exception.
There was a problem hiding this comment.
I created ApiExceptionFactory.createException(message, cause, statusCode, canRetry, errorDetails)
I think the existing message is built by HTTP transport so it looks like: "403 Forbidden (dump of exception info)"; I think the AIP 193 message is the right one for production logs and should exclude redundant and very detailed debug info.
| .build(); | ||
| } | ||
|
|
||
| if (status.getDetailsCount() > 0) { |
There was a problem hiding this comment.
ErrorDetails can handle empty details list properly, I don't think we need this check. However, we would lose message if we always call ApiExceptionFactory.createException(cause, statusCode, canRetry, errorDetails). This is probably one more reason to create ApiExceptionFactory.createException(message, cause, statusCode, canRetry, errorDetails).
| if (status.getCode() > 0) { | ||
| com.google.rpc.Code rpcCode = com.google.rpc.Code.forNumber(status.getCode()); | ||
| if (rpcCode != null) { | ||
| statusCode = HttpJsonStatusCode.of(rpcCode); |
There was a problem hiding this comment.
We already have the mapping from http status code to a Gax StatusCode, which is modeled after grpc Status.
Can we keep using the existing statusCode? Or do you think it is not accurate?
There was a problem hiding this comment.
left statusCode here, thanks!
| try { | ||
| String statusStr = errorElement.getAsJsonObject().get("status").getAsString(); | ||
| com.google.rpc.Code rpcCode = com.google.rpc.Code.valueOf(statusStr); | ||
| statusBuilder.setCode(rpcCode.getNumber()); |
There was a problem hiding this comment.
It seems a little hacky to override the response directly. I would rather use the current statusCode as I mentioned in another comment.
If we really need this info, I would suggest returning this info separately. For example, an object that wraps both Status and grpcStatusCode.
There was a problem hiding this comment.
I left the current statusCode here, thank you!
d03cdc3 to
573da98
Compare
No description provided.