Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added response headers to GitLabApiException #973

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/main/java/org/gitlab4j/api/GitLabApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +28,8 @@ public class GitLabApiException extends Exception {
private int httpStatus;
private String message;
private Map<String, List<String>> validationErrors;

private MultivaluedMap<String, String> headers;

/**
* Create a GitLabApiException instance with the specified message.
*
Expand Down Expand Up @@ -59,6 +62,7 @@ public GitLabApiException(Response response) {
super();
statusInfo = response.getStatusInfo();
httpStatus = response.getStatus();
headers = response.getStringHeaders();

if (response.hasEntity()) {

Expand Down Expand Up @@ -87,7 +91,7 @@ public GitLabApiException(Response response) {
while(fields.hasNext()) {

Entry<String, JsonNode> field = fields.next();
String fieldName = field.getKey();
String fieldName = field.getKey();
List<String> values = new ArrayList<>();
validationErrors.put(fieldName, values);
for (JsonNode value : field.getValue()) {
Expand Down Expand Up @@ -186,16 +190,25 @@ public boolean hasValidationErrors() {
}

/**
* 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.
*
* @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
*/
public Map<String, List<String>> 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<String, String> getHeaders() {
return (headers);
}

@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -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;
}

Expand Down Expand Up @@ -248,6 +262,10 @@ public boolean equals(Object obj) {
return false;
}

if (!Objects.equals(this.headers, other.headers)) {
return false;
}

return true;
}
}
2 changes: 2 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGitLabApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

Expand Down