Skip to content

Commit

Permalink
added response headers to request not ok exception (#162)
Browse files Browse the repository at this point in the history
* added response headers to RequestNotOkException

* fixed missing whitespace
  • Loading branch information
Walti91 authored Jan 4, 2024
1 parent 5cdd73a commit d5d8d40
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/spotify/github/v3/clients/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -832,12 +832,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<String, List<String>> 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<Response> processPossibleRedirects(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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<String, List<String>> headers) {
super(method, path, statusCode, msg, headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@
*/
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 {

private final int statusCode;
private final String method;
private final String path;
private final String msg;
private final Map<String, List<String>> headers;

private static String decoratedMessage(
final String method, final String path, final int statusCode, final String msg) {
Expand All @@ -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<String, List<String>> headers) {
super(decoratedMessage(method, path, statusCode, msg));
this.statusCode = statusCode;
this.method = method;
this.path = path;
this.msg = msg;
this.headers = headers;
}

/**
Expand Down Expand Up @@ -99,4 +104,13 @@ public String method() {
public String path() {
return path;
}

/**
* Get response headers
*
* @return headers
*/
public Map<String, List<String>> headers() {
return headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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.Mockito.mock;
Expand All @@ -45,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);
Expand All @@ -56,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -41,11 +42,13 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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;
Expand Down Expand Up @@ -134,6 +137,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"),
Expand All @@ -158,6 +162,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"));
Expand Down

0 comments on commit d5d8d40

Please sign in to comment.