Unverified Commit 3cd0ab92 authored by Florian Waltenberger's avatar Florian Waltenberger Committed by GitHub
Browse files

added response headers to GitLabApiException (#973)

parent 0f312e60
...@@ -10,7 +10,9 @@ import java.util.Map.Entry; ...@@ -10,7 +10,9 @@ import java.util.Map.Entry;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.StatusType; import javax.ws.rs.core.Response.StatusType;
import javax.ws.rs.core.MultivaluedMap;
import java.util.Objects;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
...@@ -26,7 +28,8 @@ public class GitLabApiException extends Exception { ...@@ -26,7 +28,8 @@ public class GitLabApiException extends Exception {
private int httpStatus; private int httpStatus;
private String message; private String message;
private Map<String, List<String>> validationErrors; private Map<String, List<String>> validationErrors;
private MultivaluedMap<String, String> headers;
/** /**
* Create a GitLabApiException instance with the specified message. * Create a GitLabApiException instance with the specified message.
* *
...@@ -59,6 +62,7 @@ public class GitLabApiException extends Exception { ...@@ -59,6 +62,7 @@ public class GitLabApiException extends Exception {
super(); super();
statusInfo = response.getStatusInfo(); statusInfo = response.getStatusInfo();
httpStatus = response.getStatus(); httpStatus = response.getStatus();
headers = response.getStringHeaders();
if (response.hasEntity()) { if (response.hasEntity()) {
...@@ -87,7 +91,7 @@ public class GitLabApiException extends Exception { ...@@ -87,7 +91,7 @@ public class GitLabApiException extends Exception {
while(fields.hasNext()) { while(fields.hasNext()) {
Entry<String, JsonNode> field = fields.next(); Entry<String, JsonNode> field = fields.next();
String fieldName = field.getKey(); String fieldName = field.getKey();
List<String> values = new ArrayList<>(); List<String> values = new ArrayList<>();
validationErrors.put(fieldName, values); validationErrors.put(fieldName, values);
for (JsonNode value : field.getValue()) { for (JsonNode value : field.getValue()) {
...@@ -186,16 +190,25 @@ public class GitLabApiException extends Exception { ...@@ -186,16 +190,25 @@ public class GitLabApiException extends Exception {
} }
/** /**
* Returns a Map&lt;String, List&lt;String&gt;&gt; instance containing validation errors if this GitLabApiException * Returns a Map&lt;String, List&lt;String&gt;&gt; instance containing validation errors if this GitLabApiException
* was caused by validation errors on the GitLab server, otherwise returns null. * was caused by validation errors on the GitLab server, otherwise returns null.
* *
* @return a Map&lt;String, List&lt;String&gt;&gt; instance containing validation errors if this GitLabApiException * @return a Map&lt;String, List&lt;String&gt;&gt; instance containing validation errors if this GitLabApiException
* was caused by validation errors on the GitLab server, otherwise returns null * was caused by validation errors on the GitLab server, otherwise returns null
*/ */
public Map<String, List<String>> getValidationErrors() { public Map<String, List<String>> getValidationErrors() {
return (validationErrors); 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<String, String> getHeaders() {
return (headers);
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
...@@ -204,6 +217,7 @@ public class GitLabApiException extends Exception { ...@@ -204,6 +217,7 @@ public class GitLabApiException extends Exception {
result = prime * result + ((message == null) ? 0 : message.hashCode()); result = prime * result + ((message == null) ? 0 : message.hashCode());
result = prime * result + ((statusInfo == null) ? 0 : statusInfo.hashCode()); result = prime * result + ((statusInfo == null) ? 0 : statusInfo.hashCode());
result = prime * result + ((validationErrors == null) ? 0 : validationErrors.hashCode()); result = prime * result + ((validationErrors == null) ? 0 : validationErrors.hashCode());
result = prime * result + ((headers == null) ? 0 : headers.hashCode());
return result; return result;
} }
...@@ -248,6 +262,10 @@ public class GitLabApiException extends Exception { ...@@ -248,6 +262,10 @@ public class GitLabApiException extends Exception {
return false; return false;
} }
if (!Objects.equals(this.headers, other.headers)) {
return false;
}
return true; return true;
} }
} }
...@@ -84,6 +84,8 @@ public class TestGitLabApiException extends AbstractIntegrationTest { ...@@ -84,6 +84,8 @@ public class TestGitLabApiException extends AbstractIntegrationTest {
assertFalse(gae.hasValidationErrors()); assertFalse(gae.hasValidationErrors());
assertEquals(404, gae.getHttpStatus()); assertEquals(404, gae.getHttpStatus());
assertTrue(gae.getMessage().contains("404")); 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()));
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment