Skip to content

Commit 15111a1

Browse files
authored
fix: include request method and URL into HttpResponseException message (#1002)
* fix: include request URL into HttpResponseException message * Add request method to the exception message + review fixes
1 parent 027c227 commit 15111a1

File tree

2 files changed

+107
-64
lines changed

2 files changed

+107
-64
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpResponseException.java

+11
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ public static StringBuilder computeMessageBuffer(HttpResponse response) {
284284
}
285285
builder.append(statusMessage);
286286
}
287+
HttpRequest request = response.getRequest();
288+
if (request != null) {
289+
if (builder.length() > 0) {
290+
builder.append('\n');
291+
}
292+
String requestMethod = request.getRequestMethod();
293+
if (requestMethod != null) {
294+
builder.append(requestMethod).append(' ');
295+
}
296+
builder.append(request.getUrl());
297+
}
287298
return builder;
288299
}
289300
}

google-http-client/src/test/java/com/google/api/client/http/HttpResponseExceptionTest.java

+96-64
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@
1414

1515
package com.google.api.client.http;
1616

17+
import static com.google.api.client.testing.http.HttpTesting.SIMPLE_GENERIC_URL;
18+
import static com.google.api.client.util.StringUtils.LINE_SEPARATOR;
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertThrows;
21+
1722
import com.google.api.client.http.HttpResponseException.Builder;
18-
import com.google.api.client.testing.http.HttpTesting;
1923
import com.google.api.client.testing.http.MockHttpTransport;
2024
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
2125
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
22-
import com.google.api.client.util.StringUtils;
2326
import java.io.ByteArrayInputStream;
2427
import java.io.ByteArrayOutputStream;
2528
import java.io.IOException;
2629
import java.io.ObjectInputStream;
2730
import java.io.ObjectOutput;
2831
import java.io.ObjectOutputStream;
2932
import junit.framework.TestCase;
33+
import org.junit.function.ThrowingRunnable;
3034

3135
/**
3236
* Tests {@link HttpResponseException}.
@@ -37,16 +41,15 @@ public class HttpResponseExceptionTest extends TestCase {
3741

3842
public void testConstructor() throws Exception {
3943
HttpTransport transport = new MockHttpTransport();
40-
HttpRequest request =
41-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
44+
HttpRequest request = transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
4245
HttpResponse response = request.execute();
4346
HttpHeaders headers = response.getHeaders();
44-
HttpResponseException e = new HttpResponseException(response);
45-
assertEquals("200", e.getMessage());
46-
assertNull(e.getContent());
47-
assertEquals(200, e.getStatusCode());
48-
assertNull(e.getStatusMessage());
49-
assertTrue(headers == e.getHeaders());
47+
HttpResponseException responseException = new HttpResponseException(response);
48+
assertThat(responseException).hasMessageThat().isEqualTo("200\nGET " + SIMPLE_GENERIC_URL);
49+
assertNull(responseException.getContent());
50+
assertEquals(200, responseException.getStatusCode());
51+
assertNull(responseException.getStatusMessage());
52+
assertTrue(headers == responseException.getHeaders());
5053
}
5154

5255
public void testBuilder() throws Exception {
@@ -83,11 +86,10 @@ public LowLevelHttpResponse execute() throws IOException {
8386
};
8487
}
8588
};
86-
HttpRequest request =
87-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
89+
HttpRequest request = transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
8890
HttpResponse response = request.execute();
89-
HttpResponseException e = new HttpResponseException(response);
90-
assertEquals("OK", e.getStatusMessage());
91+
HttpResponseException responseException = new HttpResponseException(response);
92+
assertEquals("OK", responseException.getStatusMessage());
9193
}
9294

9395
public void testConstructor_noStatusCode() throws Exception {
@@ -105,14 +107,18 @@ public LowLevelHttpResponse execute() throws IOException {
105107
};
106108
}
107109
};
108-
HttpRequest request =
109-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
110-
try {
111-
request.execute();
112-
fail();
113-
} catch (HttpResponseException e) {
114-
assertEquals("", e.getMessage());
115-
}
110+
final HttpRequest request =
111+
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
112+
HttpResponseException responseException =
113+
assertThrows(
114+
HttpResponseException.class,
115+
new ThrowingRunnable() {
116+
@Override
117+
public void run() throws Throwable {
118+
request.execute();
119+
}
120+
});
121+
assertThat(responseException).hasMessageThat().isEqualTo("GET " + SIMPLE_GENERIC_URL);
116122
}
117123

118124
public void testConstructor_messageButNoStatusCode() throws Exception {
@@ -131,14 +137,18 @@ public LowLevelHttpResponse execute() throws IOException {
131137
};
132138
}
133139
};
134-
HttpRequest request =
135-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
136-
try {
137-
request.execute();
138-
fail();
139-
} catch (HttpResponseException e) {
140-
assertEquals("Foo", e.getMessage());
141-
}
140+
final HttpRequest request =
141+
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
142+
HttpResponseException responseException =
143+
assertThrows(
144+
HttpResponseException.class,
145+
new ThrowingRunnable() {
146+
@Override
147+
public void run() throws Throwable {
148+
request.execute();
149+
}
150+
});
151+
assertThat(responseException).hasMessageThat().isEqualTo("Foo\nGET " + SIMPLE_GENERIC_URL);
142152
}
143153

144154
public void testComputeMessage() throws Exception {
@@ -156,10 +166,10 @@ public LowLevelHttpResponse execute() throws IOException {
156166
};
157167
}
158168
};
159-
HttpRequest request =
160-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
169+
HttpRequest request = transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
161170
HttpResponse response = request.execute();
162-
assertEquals("200 Foo", HttpResponseException.computeMessageBuffer(response).toString());
171+
assertThat(HttpResponseException.computeMessageBuffer(response).toString())
172+
.isEqualTo("200 Foo\nGET " + SIMPLE_GENERIC_URL);
163173
}
164174

165175
public void testThrown() throws Exception {
@@ -179,15 +189,25 @@ public LowLevelHttpResponse execute() throws IOException {
179189
};
180190
}
181191
};
182-
HttpRequest request =
183-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
184-
try {
185-
request.execute();
186-
fail();
187-
} catch (HttpResponseException e) {
188-
assertEquals(
189-
"404 Not Found" + StringUtils.LINE_SEPARATOR + "Unable to find resource", e.getMessage());
190-
}
192+
final HttpRequest request =
193+
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
194+
HttpResponseException responseException =
195+
assertThrows(
196+
HttpResponseException.class,
197+
new ThrowingRunnable() {
198+
@Override
199+
public void run() throws Throwable {
200+
request.execute();
201+
}
202+
});
203+
204+
assertThat(responseException)
205+
.hasMessageThat()
206+
.isEqualTo(
207+
"404 Not Found\nGET "
208+
+ SIMPLE_GENERIC_URL
209+
+ LINE_SEPARATOR
210+
+ "Unable to find resource");
191211
}
192212

193213
public void testInvalidCharset() throws Exception {
@@ -208,14 +228,21 @@ public LowLevelHttpResponse execute() throws IOException {
208228
};
209229
}
210230
};
211-
HttpRequest request =
212-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
213-
try {
214-
request.execute();
215-
fail();
216-
} catch (HttpResponseException e) {
217-
assertEquals("404 Not Found", e.getMessage());
218-
}
231+
final HttpRequest request =
232+
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
233+
HttpResponseException responseException =
234+
assertThrows(
235+
HttpResponseException.class,
236+
new ThrowingRunnable() {
237+
@Override
238+
public void run() throws Throwable {
239+
request.execute();
240+
}
241+
});
242+
243+
assertThat(responseException)
244+
.hasMessageThat()
245+
.isEqualTo("404 Not Found\nGET " + SIMPLE_GENERIC_URL);
219246
}
220247

221248
public void testUnsupportedCharset() throws Exception {
@@ -236,30 +263,35 @@ public LowLevelHttpResponse execute() throws IOException {
236263
};
237264
}
238265
};
239-
HttpRequest request =
240-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
241-
try {
242-
request.execute();
243-
fail();
244-
} catch (HttpResponseException e) {
245-
assertEquals("404 Not Found", e.getMessage());
246-
}
266+
final HttpRequest request =
267+
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
268+
HttpResponseException responseException =
269+
assertThrows(
270+
HttpResponseException.class,
271+
new ThrowingRunnable() {
272+
@Override
273+
public void run() throws Throwable {
274+
request.execute();
275+
}
276+
});
277+
assertThat(responseException)
278+
.hasMessageThat()
279+
.isEqualTo("404 Not Found\nGET " + SIMPLE_GENERIC_URL);
247280
}
248281

249282
public void testSerialization() throws Exception {
250283
HttpTransport transport = new MockHttpTransport();
251-
HttpRequest request =
252-
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
284+
HttpRequest request = transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
253285
HttpResponse response = request.execute();
254-
HttpResponseException e = new HttpResponseException(response);
286+
HttpResponseException responseException = new HttpResponseException(response);
255287
ByteArrayOutputStream out = new ByteArrayOutputStream();
256288
ObjectOutput s = new ObjectOutputStream(out);
257-
s.writeObject(e);
289+
s.writeObject(responseException);
258290
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
259291
ObjectInputStream objectInput = new ObjectInputStream(in);
260292
HttpResponseException e2 = (HttpResponseException) objectInput.readObject();
261-
assertEquals(e.getMessage(), e2.getMessage());
262-
assertEquals(e.getStatusCode(), e2.getStatusCode());
293+
assertEquals(responseException.getMessage(), e2.getMessage());
294+
assertEquals(responseException.getStatusCode(), e2.getStatusCode());
263295
assertNull(e2.getHeaders());
264296
}
265297
}

0 commit comments

Comments
 (0)