Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.

Commit d534131

Browse files
committed
added response headers to RequestNotOkException
1 parent 27e5fef commit d534131

5 files changed

Lines changed: 33 additions & 8 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,15 @@ public void onResponse(final Call call, final Response response) {
783783
private RequestNotOkException mapException(final Response res, final Request request)
784784
throws IOException {
785785
String bodyString = res.body() != null ? res.body().string() : "";
786+
Map<String, List<String>> headersMap = res.headers().toMultimap();
787+
786788
if (res.code() == FORBIDDEN) {
787789
if (bodyString.contains("Repository was archived so is read-only")) {
788-
return new ReadOnlyRepositoryException(request.method(), request.url().encodedPath(), res.code(), bodyString);
790+
return new ReadOnlyRepositoryException(request.method(), request.url().encodedPath(), res.code(), bodyString, headersMap);
789791
}
790792
}
791-
return new RequestNotOkException(request.method(), request.url().encodedPath(), res.code(), bodyString);
793+
794+
return new RequestNotOkException(request.method(), request.url().encodedPath(), res.code(), bodyString, headersMap);
792795
}
793796

794797
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
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

29-
import static org.mockito.ArgumentMatchers.anyString;
3030
import static org.mockito.Mockito.mock;
3131
import static org.mockito.Mockito.verify;
3232

@@ -46,7 +46,7 @@ public void succeed() {
4646
@Test
4747
public void fail() {
4848
final Span span = new OpenCensusSpan(wrapped);
49-
span.failure(new RequestNotOkException("method", "path", 404, "Not found"));
49+
span.failure(new RequestNotOkException("method", "path", 404, "Not found", Collections.emptyMap()));
5050
span.close();
5151

5252
verify(wrapped).setStatus(Status.UNKNOWN);
@@ -57,7 +57,7 @@ public void fail() {
5757
@Test
5858
public void failOnServerError() {
5959
final Span span = new OpenCensusSpan(wrapped);
60-
span.failure(new RequestNotOkException("method", "path", 500, "Internal Server Error"));
60+
span.failure(new RequestNotOkException("method", "path", 500, "Internal Server Error", Collections.emptyMap()));
6161
span.close();
6262

6363
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.Assert.fail;
2930
import static org.mockito.ArgumentMatchers.any;
@@ -38,11 +39,13 @@
3839
import com.spotify.github.v3.repos.RepositoryInvitation;
3940
import java.io.IOException;
4041
import java.net.URI;
42+
import java.util.List;
4143
import java.util.Optional;
4244
import java.util.concurrent.CompletableFuture;
4345
import java.util.concurrent.ExecutionException;
4446
import okhttp3.Call;
4547
import okhttp3.Callback;
48+
import okhttp3.Headers;
4649
import okhttp3.MediaType;
4750
import okhttp3.OkHttpClient;
4851
import okhttp3.Protocol;
@@ -118,6 +121,7 @@ public void testRequestNotOkException() throws Throwable {
118121

119122
final Response response = new okhttp3.Response.Builder()
120123
.code(409) // Conflict
124+
.headers(Headers.of("x-ratelimit-remaining", "0"))
121125
.body(
122126
ResponseBody.create(
123127
MediaType.get("application/json"),
@@ -142,6 +146,7 @@ public void testRequestNotOkException() throws Throwable {
142146
assertThat(e1.statusCode(), is(409));
143147
assertThat(e1.method(), is("POST"));
144148
assertThat(e1.path(), is("/repos/testorg/testrepo/merges"));
149+
assertThat(e1.headers(), hasEntry("x-ratelimit-remaining", List.of("0")));
145150
assertThat(e1.getMessage(), containsString("POST"));
146151
assertThat(e1.getMessage(), containsString("/repos/testorg/testrepo/merges"));
147152
assertThat(e1.getMessage(), containsString("Merge Conflict"));

0 commit comments

Comments
 (0)