Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add metadata field to GoogleJsonError ErrorInfo class. #2496

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.google.api.client.util.Data;
import com.google.api.client.util.Key;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Data class representing the Google JSON error response content, as documented for example in <a
Expand Down Expand Up @@ -191,6 +193,7 @@ public static class Details extends GenericJson {
@Key private String detail;
@Key private String reason;
@Key private List<ParameterViolations> parameterViolations;
@Key private Map<String, String> metadata;

public String getType() {
return type;
Expand Down Expand Up @@ -228,6 +231,14 @@ public List<ParameterViolations> getParameterViolations() {
public void setParameterViolations(List<ParameterViolations> parameterViolations) {
this.parameterViolations = ImmutableList.copyOf(parameterViolations);
}

public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
this.metadata = ImmutableMap.copyOf(metadata);
}
}

public static class ParameterViolations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.common.collect.ImmutableMap;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -199,4 +201,25 @@ public void testParse_withReasonInDetails() throws Exception {
JSON_PARSER.parse(DETAILS_ERROR), JSON_PARSER.parse(FACTORY.toString(errorResponse)));
assertNotNull(errorResponse.getDetails().get(2).getReason());
}

public void testParse_withMetadataInDetails() throws Exception {
InputStream errorContent =
GoogleJsonErrorTest.class.getResourceAsStream("errorWithMetadataInDetails.json");

HttpTransport transport =
new ErrorTransport(
new MockLowLevelHttpResponse()
.setContent(errorContent)
.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 errorResponse =
com.google.api.client.googleapis.json.GoogleJsonError.parse(FACTORY, response);

Map<String, String> metadata = errorResponse.getDetails().get(0).getMetadata();
assertEquals(metadata, ImmutableMap.of("service", "translate.googleapis.com"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"error": {
"code": 400,
"message": "API key not valid. Please pass a valid API key.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "API_KEY_INVALID",
"domain": "googleapis.com",
"metadata": {
"service": "translate.googleapis.com"
}
}
]
}
}
Loading