Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit c6527d9

Browse files
committed
fix(gax-httpjson): remove HttpJsonErrorParser status override
1 parent 28bb39d commit c6527d9

3 files changed

Lines changed: 6 additions & 38 deletions

File tree

gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonApiExceptionFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ ApiException create(Throwable throwable) {
6262
ErrorDetails errorDetails =
6363
ErrorDetails.builder().setRawErrorMessages(status.getDetailsList()).build();
6464

65+
if (message == null) {
66+
message = throwable.toString();
67+
}
68+
6569
return ApiExceptionFactory.createException(
6670
message, throwable, statusCode, canRetry, errorDetails);
6771
} else if (throwable instanceof HttpJsonStatusRuntimeException) {

gax-java/gax-httpjson/src/test/java/com/google/api/gax/httpjson/HttpJsonErrorParserTest.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void parseStatus_success() {
6161

6262
com.google.rpc.Status status = HttpJsonErrorParser.parseStatus(payload);
6363
assertThat(status).isNotNull();
64-
assertThat(status.getCode()).isEqualTo(16);
64+
assertThat(status.getCode()).isEqualTo(401);
6565
assertThat(status.getMessage())
6666
.isEqualTo("Request is missing required authentication credential.");
6767

@@ -135,40 +135,4 @@ void parseStatus_arrayInError() {
135135
assertThat(HttpJsonErrorParser.parseStatus(payload))
136136
.isEqualTo(com.google.rpc.Status.getDefaultInstance());
137137
}
138-
}
139-
@Test
140-
void parseStatus_withHttpCodeAndGrpcStatusString() {
141-
// AIP-193 standard JSON mapping typically includes the HTTP code in "code"
142-
// and the gRPC status string in "status". Let's verify what JsonFormat actually extracts
143-
// to the `com.google.rpc.Status` proto when both are present.
144-
String payload =
145-
"{\n"
146-
+ " \"error\": {\n"
147-
+ " \"code\": 403,\n"
148-
+ " \"status\": \"PERMISSION_DENIED\",\n"
149-
+ " \"message\": \"The caller does not have permission\"\n"
150-
+ " }\n"
151-
+ "}";
152-
153-
com.google.rpc.Status status = HttpJsonErrorParser.parseStatus(payload);
154-
155-
// In Protobuf, com.google.rpc.Status ONLY has `int32 code = 1;` and `string message = 2;`
156-
// It does NOT have a `status` field. Because we use `.ignoringUnknownFields()` in the parser,
157-
// the "status": "PERMISSION_DENIED" string is completely thrown away natively.
158-
// However, our parser manually intercepts the 'status' string to override the gRPC integer.
159-
// So we expect 7 (PERMISSION_DENIED), not 403!
160-
assertThat(status.getCode()).isEqualTo(7);
161-
assertThat(status.getMessage()).isEqualTo("The caller does not have permission");
162-
}
163-
164-
@Test
165-
void parseStatus_withOnlyStatusString() {
166-
String payload = "{\n" + " \"error\": {\n" + " \"status\": \"NOT_FOUND\"\n" + " }\n" + "}";
167-
168-
com.google.rpc.Status status = HttpJsonErrorParser.parseStatus(payload);
169-
170-
// Because "code" is missing, JsonFormat sets it to 0 (OK). But our manual override
171-
// sees "status": "NOT_FOUND" and correctly maps it to 5.
172-
assertThat(status.getCode()).isEqualTo(5);
173-
}
174138
}

gax-java/gax/src/main/java/com/google/api/gax/rpc/ApiExceptionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static ApiException createException(
8383

8484
public static ApiException createException(
8585
Throwable cause, StatusCode statusCode, boolean retryable, ErrorDetails errorDetails) {
86-
return createException(null, cause, statusCode, retryable, errorDetails);
86+
return createException(cause == null ? null : cause.toString(), cause, statusCode, retryable, errorDetails);
8787
}
8888

8989
public static ApiException createException(

0 commit comments

Comments
 (0)