Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: include request URL into GoogleJsonResponseException message
  • Loading branch information
medb committed Mar 16, 2020
commit 6eb2be2cb83e4e292ebaf192c44940d14b563745
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public static GoogleJsonResponseException from(JsonFactory jsonFactory, HttpResp
}
// message
StringBuilder message = HttpResponseException.computeMessageBuffer(response);
(message.length() == 0 ? message : message.append(StringUtils.LINE_SEPARATOR))
.append("Request URL: ")
.append(response.getRequest().getUrl());
if (!com.google.api.client.util.Strings.isNullOrEmpty(detailString)) {
message.append(StringUtils.LINE_SEPARATOR).append(detailString);
builder.setContent(detailString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.api.client.googleapis.json;

import com.google.api.client.googleapis.json.GoogleJsonErrorTest.ErrorTransport;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void testFrom_noDetails() throws Exception {
GoogleJsonResponseException ge =
GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
assertNull(ge.getDetails());
assertEquals("200", ge.getMessage());
assertEquals("200" + getRequestUrlMessage(HttpTesting.SIMPLE_GENERIC_URL), ge.getMessage());
}

public void testFrom_withDetails() throws Exception {
Expand All @@ -55,7 +56,13 @@ public void testFrom_withDetails() throws Exception {
GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
assertEquals(GoogleJsonErrorTest.ERROR, GoogleJsonErrorTest.FACTORY.toString(ge.getDetails()));
assertTrue(
ge.getMessage(), ge.getMessage().startsWith("403" + StringUtils.LINE_SEPARATOR + "{"));
ge.getMessage(),
ge.getMessage()
.startsWith(
"403"
+ getRequestUrlMessage(HttpTesting.SIMPLE_GENERIC_URL)
+ StringUtils.LINE_SEPARATOR
+ "{"));
}

public void testFrom_detailsMissingContent() throws Exception {
Expand All @@ -67,7 +74,7 @@ public void testFrom_detailsMissingContent() throws Exception {
GoogleJsonResponseException ge =
GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
assertNull(ge.getDetails());
assertEquals("403", ge.getMessage());
assertEquals("403" + getRequestUrlMessage(HttpTesting.SIMPLE_GENERIC_URL), ge.getMessage());
}

public void testFrom_detailsArbitraryJsonContent() throws Exception {
Expand All @@ -79,7 +86,7 @@ public void testFrom_detailsArbitraryJsonContent() throws Exception {
GoogleJsonResponseException ge =
GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
assertNull(ge.getDetails());
assertEquals("403", ge.getMessage());
assertEquals("403" + getRequestUrlMessage(HttpTesting.SIMPLE_GENERIC_URL), ge.getMessage());
}

public void testFrom_detailsArbitraryXmlContent() throws Exception {
Expand All @@ -92,7 +99,13 @@ public void testFrom_detailsArbitraryXmlContent() throws Exception {
GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
assertNull(ge.getDetails());
assertTrue(
ge.getMessage(), ge.getMessage().startsWith("403" + StringUtils.LINE_SEPARATOR + "<"));
ge.getMessage(),
ge.getMessage()
.startsWith(
"403"
+ getRequestUrlMessage(HttpTesting.SIMPLE_GENERIC_URL)
+ StringUtils.LINE_SEPARATOR
+ "<"));
}

public void testFrom_errorNoContentButWithJsonContentType() throws Exception {
Expand All @@ -104,7 +117,7 @@ public void testFrom_errorNoContentButWithJsonContentType() throws Exception {
GoogleJsonResponseException ge =
GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
assertNull(ge.getDetails());
assertEquals("403", ge.getMessage());
assertEquals("403" + getRequestUrlMessage(HttpTesting.SIMPLE_GENERIC_URL), ge.getMessage());
}

public void testFrom_errorEmptyContentButWithJsonContentType() throws Exception {
Expand All @@ -116,7 +129,7 @@ public void testFrom_errorEmptyContentButWithJsonContentType() throws Exception
GoogleJsonResponseException ge =
GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
assertNull(ge.getDetails());
assertEquals("403", ge.getMessage());
assertEquals("403" + getRequestUrlMessage(HttpTesting.SIMPLE_GENERIC_URL), ge.getMessage());
}

public void testFrom_detailsErrorObject() throws Exception {
Expand Down Expand Up @@ -154,6 +167,10 @@ public void testFrom_detailsNoErrorField() throws Exception {
GoogleJsonResponseException ge =
GoogleJsonResponseException.from(GoogleJsonErrorTest.FACTORY, response);
assertNull(ge.getDetails());
assertEquals("403", ge.getMessage());
assertEquals("403" + getRequestUrlMessage(HttpTesting.SIMPLE_GENERIC_URL), ge.getMessage());
}

private static String getRequestUrlMessage(GenericUrl requestUrl) {
return StringUtils.LINE_SEPARATOR + "Request URL: " + requestUrl;
}
}