Skip to content

Commit 5e92b4c

Browse files
authored
fix: add reason field to Details in GoogleJsonErrorObject (#1831)
Fixes b/191502543 google-api-java-client id parsing error objects without `reason` field in `details`. The actual error message: ``` { "error": { "code": 400, "message": "The subscription is in a state that is not valid for the requested operation", "status": "FAILED_PRECONDITION", "details": [ { "@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "ERROR_CODE_SUBSCRIPTION_BAD_STATE" } ] } } ``` But when it get parsed through java api-client, it becomes: ``` { "code" : 400, "details" : [ { "@type" : "type.googleapis.com/google.rpc.ErrorInfo" } ], "message" : "The subscription is in a state that is not valid for the requested operation", "status" : "FAILED_PRECONDITION" } ```
1 parent 405c699 commit 5e92b4c

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

Diff for: google-api-client/src/main/java/com/google/api/client/googleapis/json/GoogleJsonError.java

+9
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ public static class Details {
189189
private String type;
190190

191191
@Key private String detail;
192+
@Key private String reason;
192193
@Key private List<ParameterViolations> parameterViolations;
193194

194195
public String getType() {
@@ -207,6 +208,14 @@ public void setDetail(String detail) {
207208
this.detail = detail;
208209
}
209210

211+
public String getReason() {
212+
return reason;
213+
}
214+
215+
public void setReason(String reason) {
216+
this.reason = reason;
217+
}
218+
210219
public List<ParameterViolations> getParameterViolations() {
211220
return parameterViolations;
212221
}

Diff for: google-api-client/src/test/java/com/google/api/client/googleapis/json/GoogleJsonErrorTest.java

+44
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,48 @@ public void testParse_withDetails() throws Exception {
125125
assertEquals(DETAILS_ERROR, FACTORY.toString(errorResponse));
126126
assertNotNull(errorResponse.getDetails());
127127
}
128+
129+
public void testParse_withReasonInDetails() throws Exception {
130+
String DETAILS_ERROR =
131+
"{"
132+
+ "\"code\":400,"
133+
+ "\"details\":"
134+
+ "[{"
135+
+ "\"@type\":\"type.googleapis.com/google.dataflow.v1beta3.InvalidTemplateParameters\","
136+
+ "\"parameterViolations\":[{"
137+
+ "\"description\":\"Parameter didn't match regex '^[0-9a-zA-Z_]+$'\","
138+
+ "\"parameter\":\"safeBrowsingApiKey\""
139+
+ "}],"
140+
+ "\"reason\":\"TEST REASON 1\""
141+
+ "},{"
142+
+ "\"@type\":\"type.googleapis.com/google.rpc.DebugInfo\","
143+
+ "\"detail\":\"test detail\""
144+
+ "},{"
145+
+ "\"@type\":\"type.googleapis.com/google.rpc.DebugInfo\","
146+
+ "\"reason\":\"test reason 2\""
147+
+ "},{"
148+
+ "\"@type\":\"type.googleapis.com/google.rpc.DebugInfo\""
149+
+ "}],"
150+
+ "\"message\":\"The template parameters are invalid.\","
151+
+ "\"status\":\"INVALID_ARGUMENT\""
152+
+ "}";
153+
InputStream errorContent =
154+
GoogleJsonErrorTest.class.getResourceAsStream("errorWithReasonInDetails.json");
155+
156+
HttpTransport transport =
157+
new ErrorTransport(
158+
new MockLowLevelHttpResponse()
159+
.setContent(errorContent)
160+
.setContentType(Json.MEDIA_TYPE)
161+
.setStatusCode(HttpStatusCodes.STATUS_CODE_FORBIDDEN));
162+
HttpRequest request =
163+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
164+
request.setThrowExceptionOnExecuteError(false);
165+
HttpResponse response = request.execute();
166+
com.google.api.client.googleapis.json.GoogleJsonError errorResponse =
167+
com.google.api.client.googleapis.json.GoogleJsonError.parse(FACTORY, response);
168+
169+
assertEquals(DETAILS_ERROR, FACTORY.toString(errorResponse));
170+
assertNotNull(errorResponse.getDetails().get(2).getReason());
171+
}
128172
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"error": {
3+
"code": 400,
4+
"message": "The template parameters are invalid.",
5+
"status": "INVALID_ARGUMENT",
6+
"details": [
7+
{
8+
"@type": "type.googleapis.com/google.dataflow.v1beta3.InvalidTemplateParameters",
9+
"reason": "TEST REASON 1",
10+
"parameterViolations": [
11+
{
12+
"parameter": "safeBrowsingApiKey",
13+
"description": "Parameter didn't match regex '^[0-9a-zA-Z_]+$'"
14+
}
15+
]
16+
},
17+
{
18+
"@type": "type.googleapis.com/google.rpc.DebugInfo",
19+
"detail": "test detail"
20+
},
21+
{
22+
"@type": "type.googleapis.com/google.rpc.DebugInfo",
23+
"reason": "test reason 2"
24+
},
25+
{
26+
"@type": "type.googleapis.com/google.rpc.DebugInfo"
27+
}
28+
]
29+
}
30+
}
31+

Diff for: pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<packaging>pom</packaging>
99
<name>Parent for the Google API Client Library for Java</name>
1010
<description>The Google APIs Client Library for Java is a Java client library
11-
for accessing any HTTP-based API on the web</description>
11+
for accessing any HTTP-based API on the web</description>
1212
<url>https://github.com/googleapis/google-api-java-client</url>
1313

1414
<issueManagement>
@@ -17,7 +17,7 @@
1717
</issueManagement>
1818

1919
<inceptionYear>2010</inceptionYear>
20-
20+
2121
<developers>
2222
<developer>
2323
<id>chingor</id>

0 commit comments

Comments
 (0)