Skip to content

Commit ec3b60a

Browse files
committed
impl(gax-httpjson): return empty ErrorDetails for garbage data instead of throwing RuntimeException
1 parent 8029732 commit ec3b60a

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ static ErrorDetails parseErrorDetails(String errorJson) {
101101
}
102102

103103
JsonElement errorElement = root.get("error");
104+
if (!errorElement.isJsonObject()) {
105+
return ErrorDetails.builder().build();
106+
}
107+
104108
Status.Builder statusBuilder = Status.newBuilder();
105109
JSON_PARSER.merge(errorElement.toString(), statusBuilder);
106110
Status status = statusBuilder.build();

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,12 @@ void parseErrorDetails_noDetails() {
111111
@Test
112112
void parseErrorDetails_garbageInError() {
113113
String payload = "{\"error\": \"not-an-object\"}";
114-
Assertions.assertThrows(
115-
RuntimeException.class, () -> HttpJsonErrorParser.parseErrorDetails(payload));
114+
assertThat(HttpJsonErrorParser.parseErrorDetails(payload).getErrorInfo()).isNull();
116115
}
117116

118117
@Test
119118
void parseErrorDetails_arrayInError() {
120119
String payload = "{\"error\": []}";
121-
Assertions.assertThrows(
122-
RuntimeException.class, () -> HttpJsonErrorParser.parseErrorDetails(payload));
120+
assertThat(HttpJsonErrorParser.parseErrorDetails(payload).getErrorInfo()).isNull();
123121
}
124122
}

0 commit comments

Comments
 (0)