Skip to content

Commit

Permalink
chore: Add metadata field to GoogleJsonError ErrorInfo class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruomengz committed Jun 10, 2024
1 parent b3df1f6 commit 6e2fb88
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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"
}
}
]
}
}

0 comments on commit 6e2fb88

Please sign in to comment.