Skip to content

Commit feeb3ab

Browse files
committed
Now customizes the message for validation errors (#260).
1 parent b22a930 commit feeb3ab

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,28 @@ public GitLabApiException(Response response) {
6969
// If the node is an object, then it is validation errors
7070
if (jsonMessage.isObject()) {
7171

72+
StringBuilder buf = new StringBuilder();
7273
validationErrors = new HashMap<>();
7374
Iterator<Entry<String, JsonNode>> fields = jsonMessage.fields();
7475
while(fields.hasNext()) {
7576

7677
Entry<String, JsonNode> field = fields.next();
78+
String fieldName = field.getKey();
7779
List<String> values = new ArrayList<>();
78-
validationErrors.put(field.getKey(), values);
80+
validationErrors.put(fieldName, values);
7981
for (JsonNode value : field.getValue()) {
8082
values.add(value.asText());
8183
}
84+
85+
if (values.size() > 0) {
86+
buf.append((buf.length() > 0 ? ", " : "")).append(fieldName);
87+
}
8288
}
8389

90+
if (buf.length() > 0) {
91+
this.message = "The following fields have validation errors: " + buf.toString();
92+
}
93+
8494
} else {
8595
this.message = jsonMessage.asText();
8696
}
@@ -131,7 +141,7 @@ public final String getReason() {
131141

132142
/**
133143
* Returns the HTTP status code that was the cause of the exception. returns 0 if the
134-
* causing error was not an HTTP related exception.
144+
* causing error was not an HTTP related exception
135145
*
136146
* @return the HTTP status code, returns 0 if the causing error was not an HTTP related exception
137147
*/

0 commit comments

Comments
 (0)