From d534131f8215cbfd2bed667caa82c85da77299bb Mon Sep 17 00:00:00 2001 From: Florian Waltenberger Date: Wed, 3 May 2023 13:57:19 +0200 Subject: [PATCH 1/2] added response headers to RequestNotOkException --- .../spotify/github/v3/clients/GitHubClient.java | 7 +++++-- .../exceptions/ReadOnlyRepositoryException.java | 7 +++++-- .../v3/exceptions/RequestNotOkException.java | 16 +++++++++++++++- .../github/opencensus/OpenCensusSpanTest.java | 6 +++--- .../github/v3/clients/GitHubClientTest.java | 5 +++++ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java index cad58165..bd086dfc 100644 --- a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java +++ b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java @@ -783,12 +783,15 @@ public void onResponse(final Call call, final Response response) { private RequestNotOkException mapException(final Response res, final Request request) throws IOException { String bodyString = res.body() != null ? res.body().string() : ""; + Map> headersMap = res.headers().toMultimap(); + if (res.code() == FORBIDDEN) { if (bodyString.contains("Repository was archived so is read-only")) { - return new ReadOnlyRepositoryException(request.method(), request.url().encodedPath(), res.code(), bodyString); + return new ReadOnlyRepositoryException(request.method(), request.url().encodedPath(), res.code(), bodyString, headersMap); } } - return new RequestNotOkException(request.method(), request.url().encodedPath(), res.code(), bodyString); + + return new RequestNotOkException(request.method(), request.url().encodedPath(), res.code(), bodyString, headersMap); } CompletableFuture processPossibleRedirects( diff --git a/src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java b/src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java index 91919091..a6d318ea 100644 --- a/src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java +++ b/src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java @@ -20,6 +20,9 @@ package com.spotify.github.v3.exceptions; +import java.util.List; +import java.util.Map; + /** The Read only repository exception. */ public class ReadOnlyRepositoryException extends RequestNotOkException { /** @@ -31,7 +34,7 @@ public class ReadOnlyRepositoryException extends RequestNotOkException { * @param msg the msg */ public ReadOnlyRepositoryException( - final String method, final String path, final int statusCode, final String msg) { - super(method, path, statusCode, msg); + final String method, final String path, final int statusCode, final String msg, final Map>headers) { + super(method, path, statusCode, msg, headers); } } diff --git a/src/main/java/com/spotify/github/v3/exceptions/RequestNotOkException.java b/src/main/java/com/spotify/github/v3/exceptions/RequestNotOkException.java index c5852a54..ef6e8815 100644 --- a/src/main/java/com/spotify/github/v3/exceptions/RequestNotOkException.java +++ b/src/main/java/com/spotify/github/v3/exceptions/RequestNotOkException.java @@ -34,6 +34,9 @@ */ package com.spotify.github.v3.exceptions; +import java.util.List; +import java.util.Map; + /** HTTP response with non-200 StatusCode. */ public class RequestNotOkException extends GithubException { @@ -41,6 +44,7 @@ public class RequestNotOkException extends GithubException { private final String method; private final String path; private final String msg; + private final Map> headers; private static String decoratedMessage( final String method, final String path, final int statusCode, final String msg) { @@ -56,12 +60,13 @@ private static String decoratedMessage( * @param msg response body */ public RequestNotOkException( - final String method, final String path, final int statusCode, final String msg) { + final String method, final String path, final int statusCode, final String msg, final Map> headers) { super(decoratedMessage(method, path, statusCode, msg)); this.statusCode = statusCode; this.method = method; this.path = path; this.msg = msg; + this.headers = headers; } /** @@ -99,4 +104,13 @@ public String method() { public String path() { return path; } + + /** + * Get response headers + * + * @return headers + */ + public Map> headers() { + return headers; + } } diff --git a/src/test/java/com/spotify/github/opencensus/OpenCensusSpanTest.java b/src/test/java/com/spotify/github/opencensus/OpenCensusSpanTest.java index 73f9c354..c6b5d94d 100644 --- a/src/test/java/com/spotify/github/opencensus/OpenCensusSpanTest.java +++ b/src/test/java/com/spotify/github/opencensus/OpenCensusSpanTest.java @@ -24,9 +24,9 @@ import com.spotify.github.v3.exceptions.RequestNotOkException; import io.opencensus.trace.AttributeValue; import io.opencensus.trace.Status; +import java.util.Collections; import org.junit.jupiter.api.Test; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -46,7 +46,7 @@ public void succeed() { @Test public void fail() { final Span span = new OpenCensusSpan(wrapped); - span.failure(new RequestNotOkException("method", "path", 404, "Not found")); + span.failure(new RequestNotOkException("method", "path", 404, "Not found", Collections.emptyMap())); span.close(); verify(wrapped).setStatus(Status.UNKNOWN); @@ -57,7 +57,7 @@ public void fail() { @Test public void failOnServerError() { final Span span = new OpenCensusSpan(wrapped); - span.failure(new RequestNotOkException("method", "path", 500, "Internal Server Error")); + span.failure(new RequestNotOkException("method", "path", 500, "Internal Server Error", Collections.emptyMap())); span.close(); verify(wrapped).setStatus(Status.UNKNOWN); diff --git a/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java b/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java index 6fae45dd..ed4275bb 100644 --- a/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java @@ -24,6 +24,7 @@ import static java.nio.charset.Charset.defaultCharset; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsMapContaining.hasEntry; import static org.hamcrest.core.Is.is; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; @@ -38,11 +39,13 @@ import com.spotify.github.v3.repos.RepositoryInvitation; import java.io.IOException; import java.net.URI; +import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import okhttp3.Call; import okhttp3.Callback; +import okhttp3.Headers; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Protocol; @@ -118,6 +121,7 @@ public void testRequestNotOkException() throws Throwable { final Response response = new okhttp3.Response.Builder() .code(409) // Conflict + .headers(Headers.of("x-ratelimit-remaining", "0")) .body( ResponseBody.create( MediaType.get("application/json"), @@ -142,6 +146,7 @@ public void testRequestNotOkException() throws Throwable { assertThat(e1.statusCode(), is(409)); assertThat(e1.method(), is("POST")); assertThat(e1.path(), is("/repos/testorg/testrepo/merges")); + assertThat(e1.headers(), hasEntry("x-ratelimit-remaining", List.of("0"))); assertThat(e1.getMessage(), containsString("POST")); assertThat(e1.getMessage(), containsString("/repos/testorg/testrepo/merges")); assertThat(e1.getMessage(), containsString("Merge Conflict")); From fbb9d16d24cb32420ca381f4e2df360d1f36cc59 Mon Sep 17 00:00:00 2001 From: Florian Waltenberger Date: Wed, 3 May 2023 14:24:12 +0200 Subject: [PATCH 2/2] fixed missing whitespace --- .../github/v3/exceptions/ReadOnlyRepositoryException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java b/src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java index a6d318ea..f20a691e 100644 --- a/src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java +++ b/src/main/java/com/spotify/github/v3/exceptions/ReadOnlyRepositoryException.java @@ -34,7 +34,7 @@ public class ReadOnlyRepositoryException extends RequestNotOkException { * @param msg the msg */ public ReadOnlyRepositoryException( - final String method, final String path, final int statusCode, final String msg, final Map>headers) { + final String method, final String path, final int statusCode, final String msg, final Map> headers) { super(method, path, statusCode, msg, headers); } }