Skip to content

Commit e473858

Browse files
przemekwitekdimitris-athanasiou
authored andcommitted
Implement factory methods for ValidationException (#41993)
Implement factory methods for ValidationException to make the client code more concise (1 LOC vs 3 LOC for a single error scenario)
1 parent 8d9a971 commit e473858

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/ValidationException.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,36 @@
2121
import org.elasticsearch.common.Nullable;
2222

2323
import java.util.ArrayList;
24+
import java.util.Arrays;
2425
import java.util.List;
2526

2627
/**
2728
* Encapsulates an accumulation of validation errors
2829
*/
2930
public class ValidationException extends IllegalArgumentException {
31+
32+
/**
33+
* Creates {@link ValidationException} instance initialized with given error messages.
34+
* @param error the errors to add
35+
* @return {@link ValidationException} instance
36+
*/
37+
public static ValidationException withError(String... error) {
38+
return withErrors(Arrays.asList(error));
39+
}
40+
41+
/**
42+
* Creates {@link ValidationException} instance initialized with given error messages.
43+
* @param errors the list of errors to add
44+
* @return {@link ValidationException} instance
45+
*/
46+
public static ValidationException withErrors(List<String> errors) {
47+
ValidationException e = new ValidationException();
48+
for (String error : errors) {
49+
e.addValidationError(error);
50+
}
51+
return e;
52+
}
53+
3054
private final List<String> validationErrors = new ArrayList<>();
3155

3256
/**
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.elasticsearch.test.ESTestCase;
23+
24+
import java.util.Arrays;
25+
26+
import static org.hamcrest.Matchers.contains;
27+
import static org.hamcrest.Matchers.hasSize;
28+
29+
public class ValidationExceptionTests extends ESTestCase {
30+
31+
private static final String ERROR = "some-error";
32+
private static final String OTHER_ERROR = "some-other-error";
33+
34+
public void testWithError() {
35+
ValidationException e = ValidationException.withError(ERROR, OTHER_ERROR);
36+
assertThat(e.validationErrors(), hasSize(2));
37+
assertThat(e.validationErrors(), contains(ERROR, OTHER_ERROR));
38+
}
39+
40+
public void testWithErrors() {
41+
ValidationException e = ValidationException.withErrors(Arrays.asList(ERROR, OTHER_ERROR));
42+
assertThat(e.validationErrors(), hasSize(2));
43+
assertThat(e.validationErrors(), contains(ERROR, OTHER_ERROR));
44+
}
45+
46+
public void testAddValidationError() {
47+
ValidationException e = new ValidationException();
48+
assertThat(e.validationErrors(), hasSize(0));
49+
e.addValidationError(ERROR);
50+
assertThat(e.validationErrors(), hasSize(1));
51+
assertThat(e.validationErrors(), contains(ERROR));
52+
e.addValidationError(OTHER_ERROR);
53+
assertThat(e.validationErrors(), hasSize(2));
54+
assertThat(e.validationErrors(), contains(ERROR, OTHER_ERROR));
55+
}
56+
}

0 commit comments

Comments
 (0)