Skip to content

Commit a81d63c

Browse files
committed
changed errors conform to specification
1 parent 82e37dc commit a81d63c

File tree

6 files changed

+78
-18
lines changed

6 files changed

+78
-18
lines changed

src/main/java/graphql/ExceptionWhileDataFetching.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package graphql;
22

33

4+
import graphql.language.SourceLocation;
5+
6+
import java.util.List;
7+
48
public class ExceptionWhileDataFetching implements GraphQLError {
59

610
private final Exception exception;
@@ -14,6 +18,16 @@ public Exception getException() {
1418
}
1519

1620

21+
@Override
22+
public String getMessage() {
23+
return "Exception while fetching data: " + exception.toString();
24+
}
25+
26+
@Override
27+
public List<SourceLocation> getLocations() {
28+
return null;
29+
}
30+
1731
@Override
1832
public ErrorType geErrorType() {
1933
return ErrorType.DataFetchingException;

src/main/java/graphql/GraphQLError.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package graphql;
22

33

4+
import graphql.language.SourceLocation;
5+
6+
import java.util.List;
7+
48
public interface GraphQLError {
59

10+
String getMessage();
11+
12+
List<SourceLocation> getLocations();
613

714
ErrorType geErrorType();
815

src/main/java/graphql/InvalidSyntaxError.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,33 @@
33

44
import graphql.language.SourceLocation;
55

6+
import java.util.ArrayList;
7+
import java.util.List;
8+
69
public class InvalidSyntaxError implements GraphQLError {
710

11+
private final List<SourceLocation> sourceLocations = new ArrayList<>();
12+
813
public InvalidSyntaxError(SourceLocation sourceLocation) {
9-
this.sourceLocation = sourceLocation;
14+
if (sourceLocation != null)
15+
this.sourceLocations.add(sourceLocation);
16+
}
17+
18+
public InvalidSyntaxError(List<SourceLocation> sourceLocations) {
19+
if (sourceLocations != null) {
20+
this.sourceLocations.addAll(sourceLocations);
21+
}
1022
}
1123

12-
private final SourceLocation sourceLocation;
1324

14-
public SourceLocation getSourceLocation() {
15-
return sourceLocation;
25+
@Override
26+
public String getMessage() {
27+
return "Invalid Syntax";
28+
}
29+
30+
@Override
31+
public List<SourceLocation> getLocations() {
32+
return sourceLocations;
1633
}
1734

1835
@Override
@@ -23,7 +40,7 @@ public ErrorType geErrorType() {
2340
@Override
2441
public String toString() {
2542
return "InvalidSyntaxError{" +
26-
"sourceLocation=" + sourceLocation +
43+
"sourceLocations=" + sourceLocations +
2744
'}';
2845
}
2946

src/main/java/graphql/language/SourceLocation.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
public class SourceLocation {
55

66
private final int line;
7-
private final int positionInLine;
7+
private final int column;
88

9-
public SourceLocation(int line, int positionInLine) {
9+
public SourceLocation(int line, int column) {
1010
this.line = line;
11-
this.positionInLine = positionInLine;
11+
this.column = column;
1212
}
1313

1414
public int getLine() {
1515
return line;
1616
}
1717

18-
public int getPositionInLine() {
19-
return positionInLine;
18+
public int getColumn() {
19+
return column;
2020
}
2121

2222
@Override
@@ -27,22 +27,22 @@ public boolean equals(Object o) {
2727
SourceLocation that = (SourceLocation) o;
2828

2929
if (line != that.line) return false;
30-
return positionInLine == that.positionInLine;
30+
return column == that.column;
3131

3232
}
3333

3434
@Override
3535
public int hashCode() {
3636
int result = line;
37-
result = 31 * result + positionInLine;
37+
result = 31 * result + column;
3838
return result;
3939
}
4040

4141
@Override
4242
public String toString() {
4343
return "SourceLocation{" +
4444
"line=" + line +
45-
", positionInLine=" + positionInLine +
45+
", column=" + column +
4646
'}';
4747
}
4848
}

src/main/java/graphql/validation/ValidationError.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,59 @@
55
import graphql.GraphQLError;
66
import graphql.language.SourceLocation;
77

8+
import java.util.ArrayList;
9+
import java.util.List;
10+
811
public class ValidationError implements GraphQLError {
912

1013

1114
private final ValidationErrorType validationErrorType;
12-
private final SourceLocation sourceLocation;
15+
private final List<SourceLocation> sourceLocations = new ArrayList<>();
1316
private final String description;
1417

1518
public ValidationError(ValidationErrorType validationErrorType) {
16-
this(validationErrorType, null, null);
19+
this(validationErrorType, (SourceLocation) null, null);
1720
}
1821

1922
public ValidationError(ValidationErrorType validationErrorType, SourceLocation sourceLocation, String description) {
2023
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);
2233
this.description = description;
2334
}
2435

2536
public ValidationErrorType getValidationErrorType() {
2637
return validationErrorType;
2738
}
2839

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+
2950
@Override
3051
public ErrorType geErrorType() {
3152
return ErrorType.ValidationError;
3253
}
3354

55+
3456
@Override
3557
public String toString() {
3658
return "ValidationError{" +
3759
"validationErrorType=" + validationErrorType +
38-
", sourceLocation=" + sourceLocation +
60+
", sourceLocations=" + sourceLocations +
3961
", description='" + description + '\'' +
4062
'}';
4163
}

src/test/groovy/graphql/GraphQLTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,6 @@ class GraphQLTest extends Specification {
108108
then:
109109
errors.size() == 1
110110
errors[0].geErrorType() == ErrorType.InvalidSyntax
111-
errors[0].sourceLocation == new SourceLocation(1, 8)
111+
errors[0].sourceLocations == [new SourceLocation(1, 8)]
112112
}
113113
}

0 commit comments

Comments
 (0)