Skip to content

Commit 60939b1

Browse files
authored
fix: Make Details field extends GenericJson so that GoogleJsonError should include any arbitrary error info from services (#2210)
1 parent a6a2936 commit 60939b1

File tree

6 files changed

+173
-3
lines changed

6 files changed

+173
-3
lines changed

google-api-client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
159159
<groupId>com.google.http-client</groupId>
160160
<artifactId>google-http-client</artifactId>
161161
</dependency>
162+
<dependency>
163+
<groupId>com.google.code.gson</groupId>
164+
<artifactId>gson</artifactId>
165+
<scope>test</scope>
166+
</dependency>
162167
<dependency>
163168
<groupId>com.google.protobuf</groupId>
164169
<artifactId>protobuf-java</artifactId>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public ErrorInfo clone() {
184184
}
185185
}
186186

187-
public static class Details {
187+
public static class Details extends GenericJson {
188188
@Key("@type")
189189
private String type;
190190

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
2929
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
3030
import java.io.InputStream;
31+
import java.io.InputStreamReader;
3132
import junit.framework.TestCase;
3233

3334
/**
@@ -37,6 +38,7 @@
3738
*/
3839
public class GoogleJsonErrorTest extends TestCase {
3940

41+
public static final com.google.gson.JsonParser JSON_PARSER = new com.google.gson.JsonParser();
4042
static final JsonFactory FACTORY = new GsonFactory();
4143
static final String ERROR =
4244
"{"
@@ -93,6 +95,32 @@ public void testParse() throws Exception {
9395
assertEquals(ERROR, FACTORY.toString(errorResponse));
9496
}
9597

98+
public void testParse_withMultipleErrorTypesInDetails() throws Exception {
99+
InputStream errorResponseStream =
100+
GoogleJsonErrorTest.class.getResourceAsStream(
101+
"errorResponseWithMultipleTypesInDetails.json");
102+
103+
InputStream expectedParsedErrorResponse =
104+
GoogleJsonErrorTest.class.getResourceAsStream(
105+
"expectedParsedErrorWithMultipleTypesInDetails.json");
106+
107+
HttpTransport transport =
108+
new ErrorTransport(
109+
new MockLowLevelHttpResponse()
110+
.setContent(errorResponseStream)
111+
.setContentType(Json.MEDIA_TYPE)
112+
.setStatusCode(HttpStatusCodes.STATUS_CODE_FORBIDDEN));
113+
HttpRequest request =
114+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
115+
request.setThrowExceptionOnExecuteError(false);
116+
HttpResponse response = request.execute();
117+
com.google.api.client.googleapis.json.GoogleJsonError actualParsedErrorResponse =
118+
com.google.api.client.googleapis.json.GoogleJsonError.parse(FACTORY, response);
119+
assertEquals(
120+
JSON_PARSER.parse(new InputStreamReader(expectedParsedErrorResponse)),
121+
JSON_PARSER.parse(FACTORY.toString(actualParsedErrorResponse)));
122+
}
123+
96124
public void testParse_withDetails() throws Exception {
97125
String DETAILS_ERROR =
98126
"{"
@@ -122,7 +150,8 @@ public void testParse_withDetails() throws Exception {
122150
com.google.api.client.googleapis.json.GoogleJsonError errorResponse =
123151
com.google.api.client.googleapis.json.GoogleJsonError.parse(FACTORY, response);
124152

125-
assertEquals(DETAILS_ERROR, FACTORY.toString(errorResponse));
153+
assertEquals(
154+
JSON_PARSER.parse(DETAILS_ERROR), JSON_PARSER.parse(FACTORY.toString(errorResponse)));
126155
assertNotNull(errorResponse.getDetails());
127156
}
128157

@@ -166,7 +195,8 @@ public void testParse_withReasonInDetails() throws Exception {
166195
com.google.api.client.googleapis.json.GoogleJsonError errorResponse =
167196
com.google.api.client.googleapis.json.GoogleJsonError.parse(FACTORY, response);
168197

169-
assertEquals(DETAILS_ERROR, FACTORY.toString(errorResponse));
198+
assertEquals(
199+
JSON_PARSER.parse(DETAILS_ERROR), JSON_PARSER.parse(FACTORY.toString(errorResponse)));
170200
assertNotNull(errorResponse.getDetails().get(2).getReason());
171201
}
172202
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.BadRequest",
27+
"fieldViolations": [
28+
{
29+
"field": "language_code",
30+
"description": "Field is required"
31+
}
32+
]
33+
},
34+
{
35+
"@type": "type.googleapis.com/google.chrome.policy.v1.PolicyModificationErrorDetails",
36+
"modificationErrors": [
37+
{
38+
"policyTargetKey": {
39+
"targetResource": "orgunits/03ph8a2z19ryqq8"
40+
},
41+
"policySchema": "chrome.users.BrowserThemeColor",
42+
"fieldErrors": [
43+
{
44+
"field": "browserThemeColor",
45+
"error": "Field is an invalid hex color. Only the numbers 0-9 and letters A-F are permitted."
46+
}
47+
]
48+
}
49+
]
50+
},
51+
{
52+
"@type": "type.googleapis.com/google.mybusiness.v4.ValidationError",
53+
"errorDetails": [
54+
{
55+
"code": 3,
56+
"field": "regular_hours.periods.close_time",
57+
"message": "Time field must follow hh:mm format.",
58+
"value": "25:00"
59+
}
60+
]
61+
}
62+
]
63+
}
64+
}
65+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"code": 400,
3+
"message": "The template parameters are invalid.",
4+
"status": "INVALID_ARGUMENT",
5+
"details": [
6+
{
7+
"@type": "type.googleapis.com/google.dataflow.v1beta3.InvalidTemplateParameters",
8+
"reason": "TEST REASON 1",
9+
"parameterViolations": [
10+
{
11+
"parameter": "safeBrowsingApiKey",
12+
"description": "Parameter didn't match regex '^[0-9a-zA-Z_]+$'"
13+
}
14+
]
15+
},
16+
{
17+
"@type": "type.googleapis.com/google.rpc.DebugInfo",
18+
"detail": "test detail"
19+
},
20+
{
21+
"@type": "type.googleapis.com/google.rpc.DebugInfo",
22+
"reason": "test reason 2"
23+
},
24+
{
25+
"@type": "type.googleapis.com/google.rpc.BadRequest",
26+
"fieldViolations": [
27+
{
28+
"field": "language_code",
29+
"description": "Field is required"
30+
}
31+
]
32+
},
33+
{
34+
"@type": "type.googleapis.com/google.chrome.policy.v1.PolicyModificationErrorDetails",
35+
"modificationErrors": [
36+
{
37+
"policyTargetKey": {
38+
"targetResource": "orgunits/03ph8a2z19ryqq8"
39+
},
40+
"policySchema": "chrome.users.BrowserThemeColor",
41+
"fieldErrors": [
42+
{
43+
"field": "browserThemeColor",
44+
"error": "Field is an invalid hex color. Only the numbers 0-9 and letters A-F are permitted."
45+
}
46+
]
47+
}
48+
]
49+
},
50+
{
51+
"@type": "type.googleapis.com/google.mybusiness.v4.ValidationError",
52+
"errorDetails": [
53+
{
54+
"code": 3,
55+
"field": "regular_hours.periods.close_time",
56+
"message": "Time field must follow hh:mm format.",
57+
"value": "25:00"
58+
}
59+
]
60+
}
61+
]
62+
}
63+

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@
222222
<version>${project.protobuf-java.version}</version>
223223
<scope>test</scope>
224224
</dependency>
225+
<dependency>
226+
<groupId>com.google.code.gson</groupId>
227+
<artifactId>gson</artifactId>
228+
<version>${gson.version}</version>
229+
<scope>test</scope>
230+
</dependency>
225231
</dependencies>
226232
</dependencyManagement>
227233

@@ -515,6 +521,7 @@
515521
<project.datanucleus-maven-plugin.version>4.0.3</project.datanucleus-maven-plugin.version>
516522
<project.servlet-api.version>2.5</project.servlet-api.version>
517523
<deploy.autorelease>false</deploy.autorelease>
524+
<gson.version>2.10</gson.version>
518525
</properties>
519526

520527
<profiles>

0 commit comments

Comments
 (0)