Skip to content

Commit 3cd0ab9

Browse files
authored
added response headers to GitLabApiException (#973)
1 parent 0f312e6 commit 3cd0ab9

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/main/java/org/gitlab4j/api/GitLabApiException.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import javax.ws.rs.core.MediaType;
1111
import javax.ws.rs.core.Response;
1212
import javax.ws.rs.core.Response.StatusType;
13+
import javax.ws.rs.core.MultivaluedMap;
1314

15+
import java.util.Objects;
1416
import org.gitlab4j.api.utils.JacksonJson;
1517

1618
import com.fasterxml.jackson.databind.JsonNode;
@@ -26,7 +28,8 @@ public class GitLabApiException extends Exception {
2628
private int httpStatus;
2729
private String message;
2830
private Map<String, List<String>> validationErrors;
29-
31+
private MultivaluedMap<String, String> headers;
32+
3033
/**
3134
* Create a GitLabApiException instance with the specified message.
3235
*
@@ -59,6 +62,7 @@ public GitLabApiException(Response response) {
5962
super();
6063
statusInfo = response.getStatusInfo();
6164
httpStatus = response.getStatus();
65+
headers = response.getStringHeaders();
6266

6367
if (response.hasEntity()) {
6468

@@ -87,7 +91,7 @@ public GitLabApiException(Response response) {
8791
while(fields.hasNext()) {
8892

8993
Entry<String, JsonNode> field = fields.next();
90-
String fieldName = field.getKey();
94+
String fieldName = field.getKey();
9195
List<String> values = new ArrayList<>();
9296
validationErrors.put(fieldName, values);
9397
for (JsonNode value : field.getValue()) {
@@ -186,16 +190,25 @@ public boolean hasValidationErrors() {
186190
}
187191

188192
/**
189-
* Returns a Map&lt;String, List&lt;String&gt;&gt; instance containing validation errors if this GitLabApiException
193+
* Returns a Map&lt;String, List&lt;String&gt;&gt; instance containing validation errors if this GitLabApiException
190194
* was caused by validation errors on the GitLab server, otherwise returns null.
191195
*
192-
* @return a Map&lt;String, List&lt;String&gt;&gt; instance containing validation errors if this GitLabApiException
196+
* @return a Map&lt;String, List&lt;String&gt;&gt; instance containing validation errors if this GitLabApiException
193197
* was caused by validation errors on the GitLab server, otherwise returns null
194198
*/
195199
public Map<String, List<String>> getValidationErrors() {
196200
return (validationErrors);
197201
}
198202

203+
/**
204+
* Returns the response headers. Returns null if the causing error was not a response related exception.
205+
*
206+
* @return the response headers or null.
207+
*/
208+
public final MultivaluedMap<String, String> getHeaders() {
209+
return (headers);
210+
}
211+
199212
@Override
200213
public int hashCode() {
201214
final int prime = 31;
@@ -204,6 +217,7 @@ public int hashCode() {
204217
result = prime * result + ((message == null) ? 0 : message.hashCode());
205218
result = prime * result + ((statusInfo == null) ? 0 : statusInfo.hashCode());
206219
result = prime * result + ((validationErrors == null) ? 0 : validationErrors.hashCode());
220+
result = prime * result + ((headers == null) ? 0 : headers.hashCode());
207221
return result;
208222
}
209223

@@ -248,6 +262,10 @@ public boolean equals(Object obj) {
248262
return false;
249263
}
250264

265+
if (!Objects.equals(this.headers, other.headers)) {
266+
return false;
267+
}
268+
251269
return true;
252270
}
253271
}

src/test/java/org/gitlab4j/api/TestGitLabApiException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public void testNotFoundError() throws GitLabApiException {
8484
assertFalse(gae.hasValidationErrors());
8585
assertEquals(404, gae.getHttpStatus());
8686
assertTrue(gae.getMessage().contains("404"));
87+
assertFalse(gae.getHeaders().isEmpty());
88+
assertTrue(gae.getHeaders().containsKey("X-Request-Id"), () -> "headers contains key 'X-Request-Id'. Available keys: " + String.join(", ", gae.getHeaders().keySet()));
8789
}
8890
}
8991

0 commit comments

Comments
 (0)