Skip to content

Commit d5d8d40

Browse files
authored
added response headers to request not ok exception (#162)
* added response headers to RequestNotOkException * fixed missing whitespace
1 parent 5cdd73a commit d5d8d40

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

src/main/java/com/spotify/github/v3/clients/GitHubClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,12 +832,15 @@ public void onResponse(final Call call, final Response response) {
832832
private RequestNotOkException mapException(final Response res, final Request request)
833833
throws IOException {
834834
String bodyString = res.body() != null ? res.body().string() : "";
835+
Map<String, List<String>> headersMap = res.headers().toMultimap();
836+
835837
if (res.code() == FORBIDDEN) {
836838
if (bodyString.contains("Repository was archived so is read-only")) {
837-
return new ReadOnlyRepositoryException(request.method(), request.url().encodedPath(), res.code(), bodyString);
839+
return new ReadOnlyRepositoryException(request.method(), request.url().encodedPath(), res.code(), bodyString, headersMap);
838840
}
839841
}
840-
return new RequestNotOkException(request.method(), request.url().encodedPath(), res.code(), bodyString);
842+
843+
return new RequestNotOkException(request.method(), request.url().encodedPath(), res.code(), bodyString, headersMap);
841844
}
842845

843846
CompletableFuture<Response> processPossibleRedirects(

src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
package com.spotify.github.v3.exceptions;
2222

23+
import java.util.List;
24+
import java.util.Map;
25+
2326
/** The Read only repository exception. */
2427
public class ReadOnlyRepositoryException extends RequestNotOkException {
2528
/**
@@ -31,7 +34,7 @@ public class ReadOnlyRepositoryException extends RequestNotOkException {
3134
* @param msg the msg
3235
*/
3336
public ReadOnlyRepositoryException(
34-
final String method, final String path, final int statusCode, final String msg) {
35-
super(method, path, statusCode, msg);
37+
final String method, final String path, final int statusCode, final String msg, final Map<String, List<String>> headers) {
38+
super(method, path, statusCode, msg, headers);
3639
}
3740
}

src/main/java/com/spotify/github/v3/exceptions/RequestNotOkException.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@
3434
*/
3535
package com.spotify.github.v3.exceptions;
3636

37+
import java.util.List;
38+
import java.util.Map;
39+
3740
/** HTTP response with non-200 StatusCode. */
3841
public class RequestNotOkException extends GithubException {
3942

4043
private final int statusCode;
4144
private final String method;
4245
private final String path;
4346
private final String msg;
47+
private final Map<String, List<String>> headers;
4448

4549
private static String decoratedMessage(
4650
final String method, final String path, final int statusCode, final String msg) {
@@ -56,12 +60,13 @@ private static String decoratedMessage(
5660
* @param msg response body
5761
*/
5862
public RequestNotOkException(
59-
final String method, final String path, final int statusCode, final String msg) {
63+
final String method, final String path, final int statusCode, final String msg, final Map<String, List<String>> headers) {
6064
super(decoratedMessage(method, path, statusCode, msg));
6165
this.statusCode = statusCode;
6266
this.method = method;
6367
this.path = path;
6468
this.msg = msg;
69+
this.headers = headers;
6570
}
6671

6772
/**
@@ -99,4 +104,13 @@ public String method() {
99104
public String path() {
100105
return path;
101106
}
107+
108+
/**
109+
* Get response headers
110+
*
111+
* @return headers
112+
*/
113+
public Map<String, List<String>> headers() {
114+
return headers;
115+
}
102116
}

src/test/java/com/spotify/github/opencensus/OpenCensusSpanTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.spotify.github.v3.exceptions.RequestNotOkException;
2525
import io.opencensus.trace.AttributeValue;
2626
import io.opencensus.trace.Status;
27+
import java.util.Collections;
2728
import org.junit.jupiter.api.Test;
2829

2930
import static org.mockito.Mockito.mock;
@@ -45,7 +46,7 @@ public void succeed() {
4546
@Test
4647
public void fail() {
4748
final Span span = new OpenCensusSpan(wrapped);
48-
span.failure(new RequestNotOkException("method", "path", 404, "Not found"));
49+
span.failure(new RequestNotOkException("method", "path", 404, "Not found", Collections.emptyMap()));
4950
span.close();
5051

5152
verify(wrapped).setStatus(Status.UNKNOWN);
@@ -56,7 +57,7 @@ public void fail() {
5657
@Test
5758
public void failOnServerError() {
5859
final Span span = new OpenCensusSpan(wrapped);
59-
span.failure(new RequestNotOkException("method", "path", 500, "Internal Server Error"));
60+
span.failure(new RequestNotOkException("method", "path", 500, "Internal Server Error", Collections.emptyMap()));
6061
span.close();
6162

6263
verify(wrapped).setStatus(Status.UNKNOWN);

src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static java.nio.charset.Charset.defaultCharset;
2525
import static org.hamcrest.CoreMatchers.containsString;
2626
import static org.hamcrest.MatcherAssert.assertThat;
27+
import static org.hamcrest.collection.IsMapContaining.hasEntry;
2728
import static org.hamcrest.core.Is.is;
2829
import static org.junit.jupiter.api.Assertions.assertThrows;
2930
import static org.mockito.ArgumentMatchers.any;
@@ -41,11 +42,13 @@
4142
import java.io.IOException;
4243
import java.net.URI;
4344
import java.net.URISyntaxException;
45+
import java.util.List;
4446
import java.util.Optional;
4547
import java.util.concurrent.CompletableFuture;
4648
import java.util.concurrent.ExecutionException;
4749
import okhttp3.Call;
4850
import okhttp3.Callback;
51+
import okhttp3.Headers;
4952
import okhttp3.MediaType;
5053
import okhttp3.OkHttpClient;
5154
import okhttp3.Protocol;
@@ -134,6 +137,7 @@ public void testRequestNotOkException() throws Throwable {
134137

135138
final Response response = new okhttp3.Response.Builder()
136139
.code(409) // Conflict
140+
.headers(Headers.of("x-ratelimit-remaining", "0"))
137141
.body(
138142
ResponseBody.create(
139143
MediaType.get("application/json"),
@@ -158,6 +162,7 @@ public void testRequestNotOkException() throws Throwable {
158162
assertThat(e1.statusCode(), is(409));
159163
assertThat(e1.method(), is("POST"));
160164
assertThat(e1.path(), is("/repos/testorg/testrepo/merges"));
165+
assertThat(e1.headers(), hasEntry("x-ratelimit-remaining", List.of("0")));
161166
assertThat(e1.getMessage(), containsString("POST"));
162167
assertThat(e1.getMessage(), containsString("/repos/testorg/testrepo/merges"));
163168
assertThat(e1.getMessage(), containsString("Merge Conflict"));

0 commit comments

Comments
 (0)