diff --git a/src/main/java/org/gitlab4j/api/GitLabApiException.java b/src/main/java/org/gitlab4j/api/GitLabApiException.java index 8af8ee82b..b8faeff70 100755 --- a/src/main/java/org/gitlab4j/api/GitLabApiException.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiException.java @@ -10,7 +10,9 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.StatusType; +import javax.ws.rs.core.MultivaluedMap; +import java.util.Objects; import org.gitlab4j.api.utils.JacksonJson; import com.fasterxml.jackson.databind.JsonNode; @@ -26,7 +28,8 @@ public class GitLabApiException extends Exception { private int httpStatus; private String message; private Map> validationErrors; - + private MultivaluedMap headers; + /** * Create a GitLabApiException instance with the specified message. * @@ -59,6 +62,7 @@ public GitLabApiException(Response response) { super(); statusInfo = response.getStatusInfo(); httpStatus = response.getStatus(); + headers = response.getStringHeaders(); if (response.hasEntity()) { @@ -87,7 +91,7 @@ public GitLabApiException(Response response) { while(fields.hasNext()) { Entry field = fields.next(); - String fieldName = field.getKey(); + String fieldName = field.getKey(); List values = new ArrayList<>(); validationErrors.put(fieldName, values); for (JsonNode value : field.getValue()) { @@ -186,16 +190,25 @@ public boolean hasValidationErrors() { } /** - * Returns a Map<String, List<String>> instance containing validation errors if this GitLabApiException + * Returns a Map<String, List<String>> instance containing validation errors if this GitLabApiException * was caused by validation errors on the GitLab server, otherwise returns null. * - * @return a Map<String, List<String>> instance containing validation errors if this GitLabApiException + * @return a Map<String, List<String>> instance containing validation errors if this GitLabApiException * was caused by validation errors on the GitLab server, otherwise returns null */ public Map> getValidationErrors() { return (validationErrors); } + /** + * Returns the response headers. Returns null if the causing error was not a response related exception. + * + * @return the response headers or null. + */ + public final MultivaluedMap getHeaders() { + return (headers); + } + @Override public int hashCode() { final int prime = 31; @@ -204,6 +217,7 @@ public int hashCode() { result = prime * result + ((message == null) ? 0 : message.hashCode()); result = prime * result + ((statusInfo == null) ? 0 : statusInfo.hashCode()); result = prime * result + ((validationErrors == null) ? 0 : validationErrors.hashCode()); + result = prime * result + ((headers == null) ? 0 : headers.hashCode()); return result; } @@ -248,6 +262,10 @@ public boolean equals(Object obj) { return false; } + if (!Objects.equals(this.headers, other.headers)) { + return false; + } + return true; } } diff --git a/src/test/java/org/gitlab4j/api/TestGitLabApiException.java b/src/test/java/org/gitlab4j/api/TestGitLabApiException.java index bbf17d63e..8035415be 100644 --- a/src/test/java/org/gitlab4j/api/TestGitLabApiException.java +++ b/src/test/java/org/gitlab4j/api/TestGitLabApiException.java @@ -84,6 +84,8 @@ public void testNotFoundError() throws GitLabApiException { assertFalse(gae.hasValidationErrors()); assertEquals(404, gae.getHttpStatus()); assertTrue(gae.getMessage().contains("404")); + assertFalse(gae.getHeaders().isEmpty()); + assertTrue(gae.getHeaders().containsKey("X-Request-Id"), () -> "headers contains key 'X-Request-Id'. Available keys: " + String.join(", ", gae.getHeaders().keySet())); } }