Skip to content

Commit e19601c

Browse files
authored
Add HTTP method to RequestNotOkException (#98)
Adding the HTTP method to the failure exception allows for better visibility into which call exactly failed. We already had the HTTP path in the exception but there are usually multiple HTTP methods that are valid for a single path.
1 parent 1dd9b28 commit e19601c

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,10 @@ private RequestNotOkException mapException(final Response res, final Request req
741741
String bodyString = res.body() != null ? res.body().string() : "";
742742
if (res.code() == FORBIDDEN) {
743743
if (bodyString.contains("Repository was archived so is read-only")) {
744-
return new ReadOnlyRepositoryException(request.url().encodedPath(), res.code(), bodyString);
744+
return new ReadOnlyRepositoryException(request.method(), request.url().encodedPath(), res.code(), bodyString);
745745
}
746746
}
747-
return new RequestNotOkException(request.url().encodedPath(), res.code(), bodyString);
747+
return new RequestNotOkException(request.method(), request.url().encodedPath(), res.code(), bodyString);
748748
}
749749

750750
CompletableFuture<Response> processPossibleRedirects(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,11 +25,13 @@ public class ReadOnlyRepositoryException extends RequestNotOkException {
2525
/**
2626
* Instantiates a new Read only repository exception.
2727
*
28+
* @param method HTTP method
2829
* @param path the path
2930
* @param statusCode the status code
3031
* @param msg the msg
3132
*/
32-
public ReadOnlyRepositoryException(final String path, final int statusCode, final String msg) {
33-
super(path, statusCode, msg);
33+
public ReadOnlyRepositoryException(
34+
final String method, final String path, final int statusCode, final String msg) {
35+
super(method, path, statusCode, msg);
3436
}
3537
}

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -38,23 +38,28 @@
3838
public class RequestNotOkException extends GithubException {
3939

4040
private final int statusCode;
41+
private final String method;
4142
private final String path;
4243
private final String msg;
4344

44-
private static String decoratedMessage(final String path, final int statusCode, final String msg) {
45-
return String.format("%s %d: %s", path, statusCode, msg);
45+
private static String decoratedMessage(
46+
final String method, final String path, final int statusCode, final String msg) {
47+
return String.format("%s %s %d: %s", method, path, statusCode, msg);
4648
}
4749

4850
/**
4951
* Response to request came back with non-2xx status code
5052
*
53+
* @param method HTTP method
5154
* @param path URI path
5255
* @param statusCode status of repsonse
5356
* @param msg response body
5457
*/
55-
public RequestNotOkException(final String path, final int statusCode, final String msg) {
56-
super(decoratedMessage(path, statusCode, msg));
58+
public RequestNotOkException(
59+
final String method, final String path, final int statusCode, final String msg) {
60+
super(decoratedMessage(method, path, statusCode, msg));
5761
this.statusCode = statusCode;
62+
this.method = method;
5863
this.path = path;
5964
this.msg = msg;
6065
}
@@ -77,6 +82,15 @@ public int statusCode() {
7782
return statusCode;
7883
}
7984

85+
/**
86+
* Get request HTTP method
87+
*
88+
* @return method
89+
*/
90+
public String method() {
91+
return method;
92+
}
93+
8094
/**
8195
* Get request URI path
8296
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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("path", 404, "Not found"));
49+
span.failure(new RequestNotOkException("method", "path", 404, "Not found"));
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("path", 500, "Internal Server Error"));
60+
span.failure(new RequestNotOkException("method", "path", 500, "Internal Server Error"));
6161
span.close();
6262

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ public void testRequestNotOkException() throws Throwable {
130130
assertThat(e.getCause() instanceof RequestNotOkException, is(true));
131131
RequestNotOkException e1 = (RequestNotOkException) e.getCause();
132132
assertThat(e1.statusCode(), is(409));
133+
assertThat(e1.method(), is("POST"));
134+
assertThat(e1.path(), is("/repos/testorg/testrepo/merges"));
135+
assertThat(e1.getMessage(), containsString("POST"));
136+
assertThat(e1.getMessage(), containsString("/repos/testorg/testrepo/merges"));
133137
assertThat(e1.getMessage(), containsString("Merge Conflict"));
134138
assertThat(e1.getRawMessage(), containsString("Merge Conflict"));
135139
}

0 commit comments

Comments
 (0)