Skip to content

Commit

Permalink
fix: Make Details field extends GenericJson so that GoogleJsonError s…
Browse files Browse the repository at this point in the history
…hould include any arbitrary error info from services (#2210)
  • Loading branch information
blakeli0 committed Jan 3, 2023
1 parent a6a2936 commit 60939b1
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 3 deletions.
5 changes: 5 additions & 0 deletions google-api-client/pom.xml
Expand Up @@ -159,6 +159,11 @@
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
Expand Down
Expand Up @@ -184,7 +184,7 @@ public ErrorInfo clone() {
}
}

public static class Details {
public static class Details extends GenericJson {
@Key("@type")
private String type;

Expand Down
Expand Up @@ -28,6 +28,7 @@
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import java.io.InputStream;
import java.io.InputStreamReader;
import junit.framework.TestCase;

/**
Expand All @@ -37,6 +38,7 @@
*/
public class GoogleJsonErrorTest extends TestCase {

public static final com.google.gson.JsonParser JSON_PARSER = new com.google.gson.JsonParser();
static final JsonFactory FACTORY = new GsonFactory();
static final String ERROR =
"{"
Expand Down Expand Up @@ -93,6 +95,32 @@ public void testParse() throws Exception {
assertEquals(ERROR, FACTORY.toString(errorResponse));
}

public void testParse_withMultipleErrorTypesInDetails() throws Exception {
InputStream errorResponseStream =
GoogleJsonErrorTest.class.getResourceAsStream(
"errorResponseWithMultipleTypesInDetails.json");

InputStream expectedParsedErrorResponse =
GoogleJsonErrorTest.class.getResourceAsStream(
"expectedParsedErrorWithMultipleTypesInDetails.json");

HttpTransport transport =
new ErrorTransport(
new MockLowLevelHttpResponse()
.setContent(errorResponseStream)
.setContentType(Json.MEDIA_TYPE)
.setStatusCode(HttpStatusCodes.STATUS_CODE_FORBIDDEN));
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
request.setThrowExceptionOnExecuteError(false);
HttpResponse response = request.execute();
com.google.api.client.googleapis.json.GoogleJsonError actualParsedErrorResponse =
com.google.api.client.googleapis.json.GoogleJsonError.parse(FACTORY, response);
assertEquals(
JSON_PARSER.parse(new InputStreamReader(expectedParsedErrorResponse)),
JSON_PARSER.parse(FACTORY.toString(actualParsedErrorResponse)));
}

public void testParse_withDetails() throws Exception {
String DETAILS_ERROR =
"{"
Expand Down Expand Up @@ -122,7 +150,8 @@ public void testParse_withDetails() throws Exception {
com.google.api.client.googleapis.json.GoogleJsonError errorResponse =
com.google.api.client.googleapis.json.GoogleJsonError.parse(FACTORY, response);

assertEquals(DETAILS_ERROR, FACTORY.toString(errorResponse));
assertEquals(
JSON_PARSER.parse(DETAILS_ERROR), JSON_PARSER.parse(FACTORY.toString(errorResponse)));
assertNotNull(errorResponse.getDetails());
}

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

assertEquals(DETAILS_ERROR, FACTORY.toString(errorResponse));
assertEquals(
JSON_PARSER.parse(DETAILS_ERROR), JSON_PARSER.parse(FACTORY.toString(errorResponse)));
assertNotNull(errorResponse.getDetails().get(2).getReason());
}
}
@@ -0,0 +1,65 @@
{
"error": {
"code": 400,
"message": "The template parameters are invalid.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.dataflow.v1beta3.InvalidTemplateParameters",
"reason": "TEST REASON 1",
"parameterViolations": [
{
"parameter": "safeBrowsingApiKey",
"description": "Parameter didn't match regex '^[0-9a-zA-Z_]+$'"
}
]
},
{
"@type": "type.googleapis.com/google.rpc.DebugInfo",
"detail": "test detail"
},
{
"@type": "type.googleapis.com/google.rpc.DebugInfo",
"reason": "test reason 2"
},
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "language_code",
"description": "Field is required"
}
]
},
{
"@type": "type.googleapis.com/google.chrome.policy.v1.PolicyModificationErrorDetails",
"modificationErrors": [
{
"policyTargetKey": {
"targetResource": "orgunits/03ph8a2z19ryqq8"
},
"policySchema": "chrome.users.BrowserThemeColor",
"fieldErrors": [
{
"field": "browserThemeColor",
"error": "Field is an invalid hex color. Only the numbers 0-9 and letters A-F are permitted."
}
]
}
]
},
{
"@type": "type.googleapis.com/google.mybusiness.v4.ValidationError",
"errorDetails": [
{
"code": 3,
"field": "regular_hours.periods.close_time",
"message": "Time field must follow hh:mm format.",
"value": "25:00"
}
]
}
]
}
}

@@ -0,0 +1,63 @@
{
"code": 400,
"message": "The template parameters are invalid.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.dataflow.v1beta3.InvalidTemplateParameters",
"reason": "TEST REASON 1",
"parameterViolations": [
{
"parameter": "safeBrowsingApiKey",
"description": "Parameter didn't match regex '^[0-9a-zA-Z_]+$'"
}
]
},
{
"@type": "type.googleapis.com/google.rpc.DebugInfo",
"detail": "test detail"
},
{
"@type": "type.googleapis.com/google.rpc.DebugInfo",
"reason": "test reason 2"
},
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "language_code",
"description": "Field is required"
}
]
},
{
"@type": "type.googleapis.com/google.chrome.policy.v1.PolicyModificationErrorDetails",
"modificationErrors": [
{
"policyTargetKey": {
"targetResource": "orgunits/03ph8a2z19ryqq8"
},
"policySchema": "chrome.users.BrowserThemeColor",
"fieldErrors": [
{
"field": "browserThemeColor",
"error": "Field is an invalid hex color. Only the numbers 0-9 and letters A-F are permitted."
}
]
}
]
},
{
"@type": "type.googleapis.com/google.mybusiness.v4.ValidationError",
"errorDetails": [
{
"code": 3,
"field": "regular_hours.periods.close_time",
"message": "Time field must follow hh:mm format.",
"value": "25:00"
}
]
}
]
}

7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -222,6 +222,12 @@
<version>${project.protobuf-java.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -515,6 +521,7 @@
<project.datanucleus-maven-plugin.version>4.0.3</project.datanucleus-maven-plugin.version>
<project.servlet-api.version>2.5</project.servlet-api.version>
<deploy.autorelease>false</deploy.autorelease>
<gson.version>2.10</gson.version>
</properties>

<profiles>
Expand Down

0 comments on commit 60939b1

Please sign in to comment.