|
5 | 5 | import graphql.GraphQLError; |
6 | 6 | import graphql.language.SourceLocation; |
7 | 7 |
|
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | + |
8 | 11 | public class ValidationError implements GraphQLError { |
9 | 12 |
|
10 | 13 |
|
11 | 14 | private final ValidationErrorType validationErrorType; |
12 | | - private final SourceLocation sourceLocation; |
| 15 | + private final List<SourceLocation> sourceLocations = new ArrayList<>(); |
13 | 16 | private final String description; |
14 | 17 |
|
15 | 18 | public ValidationError(ValidationErrorType validationErrorType) { |
16 | | - this(validationErrorType, null, null); |
| 19 | + this(validationErrorType, (SourceLocation) null, null); |
17 | 20 | } |
18 | 21 |
|
19 | 22 | public ValidationError(ValidationErrorType validationErrorType, SourceLocation sourceLocation, String description) { |
20 | 23 | this.validationErrorType = validationErrorType; |
21 | | - this.sourceLocation = sourceLocation; |
| 24 | + if (sourceLocation != null) |
| 25 | + this.sourceLocations.add(sourceLocation); |
| 26 | + this.description = description; |
| 27 | + } |
| 28 | + |
| 29 | + public ValidationError(ValidationErrorType validationErrorType, List<SourceLocation> sourceLocations, String description) { |
| 30 | + this.validationErrorType = validationErrorType; |
| 31 | + if (sourceLocations != null) |
| 32 | + this.sourceLocations.addAll(sourceLocations); |
22 | 33 | this.description = description; |
23 | 34 | } |
24 | 35 |
|
25 | 36 | public ValidationErrorType getValidationErrorType() { |
26 | 37 | return validationErrorType; |
27 | 38 | } |
28 | 39 |
|
| 40 | + @Override |
| 41 | + public String getMessage() { |
| 42 | + return String.format("Validation error of type %s: %s", validationErrorType, description); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public List<SourceLocation> getLocations() { |
| 47 | + return sourceLocations; |
| 48 | + } |
| 49 | + |
29 | 50 | @Override |
30 | 51 | public ErrorType geErrorType() { |
31 | 52 | return ErrorType.ValidationError; |
32 | 53 | } |
33 | 54 |
|
| 55 | + |
34 | 56 | @Override |
35 | 57 | public String toString() { |
36 | 58 | return "ValidationError{" + |
37 | 59 | "validationErrorType=" + validationErrorType + |
38 | | - ", sourceLocation=" + sourceLocation + |
| 60 | + ", sourceLocations=" + sourceLocations + |
39 | 61 | ", description='" + description + '\'' + |
40 | 62 | '}'; |
41 | 63 | } |
|
0 commit comments