Commit 4c028c4f authored by Greg Messner's avatar Greg Messner
Browse files

Mods to support error, message, and validation errors (#260).

parent 8176577f
package org.gitlab4j.api; package org.gitlab4j.api;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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 org.gitlab4j.api.models.ErrorMessage; import org.gitlab4j.api.utils.JacksonJson;
import com.fasterxml.jackson.databind.JsonNode;
/** /**
* This is the exception that will be thrown if any exception occurs while communicating * This is the exception that will be thrown if any exception occurs while communicating
...@@ -15,6 +25,7 @@ public class GitLabApiException extends Exception { ...@@ -15,6 +25,7 @@ public class GitLabApiException extends Exception {
private StatusType statusInfo; private StatusType statusInfo;
private int httpStatus; private int httpStatus;
private String message; private String message;
private Map<String, List<String>> validationErrors;
/** /**
* Create a GitLabApiException instance with the specified message. * Create a GitLabApiException instance with the specified message.
...@@ -28,7 +39,7 @@ public class GitLabApiException extends Exception { ...@@ -28,7 +39,7 @@ public class GitLabApiException extends Exception {
/** /**
* Create a GitLabApiException instance based on the ClientResponse. * Create a GitLabApiException instance based on the ClientResponse.
* *
* @param response the JAX-RS response that caused the exception * @param response the JAX-RS response that caused the exception
*/ */
public GitLabApiException(Response response) { public GitLabApiException(Response response) {
...@@ -38,10 +49,50 @@ public class GitLabApiException extends Exception { ...@@ -38,10 +49,50 @@ public class GitLabApiException extends Exception {
httpStatus = response.getStatus(); httpStatus = response.getStatus();
if (response.hasEntity()) { if (response.hasEntity()) {
try { try {
ErrorMessage errorMessage = response.readEntity(ErrorMessage.class); String message = response.readEntity(String.class);
message = errorMessage.getMessage(); this.message = message;
// Determine what is in the content of the response and process it accordingly
MediaType mediaType = response.getMediaType();
if (mediaType != null && "json".equals(mediaType.getSubtype())) {
JsonNode json = JacksonJson.toJsonNode(message);
// First see if it is a "message", if so it is either a simple message,
// or a Map<String, List<String>> of validation errors
JsonNode jsonMessage = json.get("message");
if (jsonMessage != null) {
// If the node is an object, then it is validation errors
if (jsonMessage.isObject()) {
validationErrors = new HashMap<>();
Iterator<Entry<String, JsonNode>> fields = jsonMessage.fields();
while(fields.hasNext()) {
Entry<String, JsonNode> field = fields.next();
List<String> values = new ArrayList<>();
validationErrors.put(field.getKey(), values);
for (JsonNode value : field.getValue()) {
values.add(value.asText());
}
}
} else {
this.message = jsonMessage.asText();
}
} else {
JsonNode jsonError = json.get("error");
if (jsonError != null) {
this.message = jsonError.asText();
}
}
}
} catch (Exception ignore) { } catch (Exception ignore) {
} }
...@@ -50,7 +101,7 @@ public class GitLabApiException extends Exception { ...@@ -50,7 +101,7 @@ public class GitLabApiException extends Exception {
/** /**
* Create a GitLabApiException instance based on the exception. * Create a GitLabApiException instance based on the exception.
* *
* @param e the Exception to wrap * @param e the Exception to wrap
*/ */
public GitLabApiException(Exception e) { public GitLabApiException(Exception e) {
...@@ -60,7 +111,7 @@ public class GitLabApiException extends Exception { ...@@ -60,7 +111,7 @@ public class GitLabApiException extends Exception {
/** /**
* Get the message associated with the exception. * Get the message associated with the exception.
* *
* @return the message associated with the exception * @return the message associated with the exception
*/ */
@Override @Override
...@@ -71,7 +122,7 @@ public class GitLabApiException extends Exception { ...@@ -71,7 +122,7 @@ public class GitLabApiException extends Exception {
/** /**
* Returns the HTTP status reason message, returns null if the * Returns the HTTP status reason message, returns null if the
* causing error was not an HTTP related exception. * causing error was not an HTTP related exception.
* *
* @return the HTTP status reason message * @return the HTTP status reason message
*/ */
public final String getReason() { public final String getReason() {
...@@ -81,10 +132,32 @@ public class GitLabApiException extends Exception { ...@@ -81,10 +132,32 @@ public class GitLabApiException extends Exception {
/** /**
* Returns the HTTP status code that was the cause of the exception. returns 0 if the * Returns the HTTP status code that was the cause of the exception. returns 0 if the
* causing error was not an HTTP related exception. * causing error was not an HTTP related exception.
* *
* @return the HTTP status code, returns 0 if the causing error was not an HTTP related exception * @return the HTTP status code, returns 0 if the causing error was not an HTTP related exception
*/ */
public final int getHttpStatus() { public final int getHttpStatus() {
return (httpStatus); return (httpStatus);
} }
/**
* Returns true if this GitLabApiException was caused by validation errors on the GitLab server,
* otherwise returns false.
*
* @return true if this GitLabApiException was caused by validation errors on the GitLab server,
* otherwise returns false
*/
public boolean hasValidationErrors() {
return (validationErrors != null);
}
/**
* 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
* was caused by validation errors on the GitLab server, otherwise returns null
*/
public Map<String, List<String>> getValidationErrors() {
return (validationErrors);
}
} }
...@@ -243,4 +243,15 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol ...@@ -243,4 +243,15 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
public static <T> String toJsonString(final T object) { public static <T> String toJsonString(final T object) {
return (JacksonJsonSingletonHelper.JACKSON_JSON.marshal(object)); return (JacksonJsonSingletonHelper.JACKSON_JSON.marshal(object));
} }
/**
* Parse the provided String into a JsonNode instance.
*
* @param jsonString a String containing JSON to parse
* @return a JsonNode with the String parsed into a JSON tree
* @throws IOException if any IO error occurs
*/
public static JsonNode toJsonNode(String json) throws IOException {
return (JacksonJsonSingletonHelper.JACKSON_JSON.objectMapper.readTree(json));
}
} }
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