Skip to content

Commit 7652086

Browse files
committed
Made the ErrorType implement a base interface
1 parent a4b9fc4 commit 7652086

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-33
lines changed

src/main/java/graphql/ErrorType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* All the errors in graphql belong to one of these categories
66
*/
77
@PublicApi
8-
public enum ErrorType {
8+
public enum ErrorType implements ErrorTypeClassification {
99
InvalidSyntax,
1010
ValidationError,
1111
DataFetchingException,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package graphql;
2+
3+
/**
4+
* Errors in graphql-java can have a classification to help with the processing
5+
* of errors. Custom {@link graphql.GraphQLError} implementations could use
6+
* custom classifications.
7+
* <p>
8+
* graphql-java ships with a standard set of error classifications via {@link graphql.ErrorType}
9+
*/
10+
@PublicApi
11+
public interface ErrorTypeClassification {
12+
}

src/main/java/graphql/GraphQLError.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public interface GraphQLError extends Serializable {
3232
List<SourceLocation> getLocations();
3333

3434
/**
35-
* @return an enum classifying this error
35+
* @return an object classifying this error
3636
*/
37-
ErrorType getErrorType();
37+
ErrorTypeClassification getErrorType();
3838

3939
/**
4040
* The graphql spec says that the (optional) path field of any error should be a list

src/main/java/graphql/GraphqlErrorBuilder.java

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class GraphqlErrorBuilder {
2121
private String message;
2222
private List<Object> path;
2323
private List<SourceLocation> locations = new ArrayList<>();
24-
private ErrorType errorType = ErrorType.DataFetchingException;
24+
private ErrorTypeClassification errorType = ErrorType.DataFetchingException;
2525
private Map<String, Object> extensions = null;
2626

2727

@@ -74,7 +74,7 @@ public GraphqlErrorBuilder path(List<Object> path) {
7474
return this;
7575
}
7676

77-
public GraphqlErrorBuilder errorType(ErrorType errorType) {
77+
public GraphqlErrorBuilder errorType(ErrorTypeClassification errorType) {
7878
this.errorType = assertNotNull(errorType);
7979
return this;
8080
}
@@ -89,32 +89,48 @@ public GraphqlErrorBuilder extensions(Map<String, Object> extensions) {
8989
*/
9090
public GraphQLError build() {
9191
assertNotNull(message, "You must provide error message");
92-
return new GraphQLError() {
93-
@Override
94-
public String getMessage() {
95-
return message;
96-
}
97-
98-
@Override
99-
public List<SourceLocation> getLocations() {
100-
return locations;
101-
}
102-
103-
@Override
104-
public ErrorType getErrorType() {
105-
return errorType;
106-
}
107-
108-
@Override
109-
public List<Object> getPath() {
110-
return path;
111-
}
112-
113-
@Override
114-
public Map<String, Object> getExtensions() {
115-
return extensions;
116-
}
117-
};
92+
return new GraphqlErrorImpl(message, locations, errorType, path, extensions);
93+
}
94+
95+
private static class GraphqlErrorImpl implements GraphQLError {
96+
private final String message;
97+
private final List<SourceLocation> locations;
98+
private final ErrorTypeClassification errorType;
99+
private final List<Object> path;
100+
private final Map<String, Object> extensions;
101+
102+
public GraphqlErrorImpl(String message, List<SourceLocation> locations, ErrorTypeClassification errorType, List<Object> path, Map<String, Object> extensions) {
103+
this.message = message;
104+
this.locations = locations;
105+
this.errorType = errorType;
106+
this.path = path;
107+
this.extensions = extensions;
108+
}
109+
110+
@Override
111+
public String getMessage() {
112+
return message;
113+
}
114+
115+
@Override
116+
public List<SourceLocation> getLocations() {
117+
return locations;
118+
}
119+
120+
@Override
121+
public ErrorTypeClassification getErrorType() {
122+
return errorType;
123+
}
124+
125+
@Override
126+
public List<Object> getPath() {
127+
return path;
128+
}
129+
130+
@Override
131+
public Map<String, Object> getExtensions() {
132+
return extensions;
133+
}
118134
}
119135

120136
/**

src/main/java/graphql/execution/AbsoluteGraphQLError.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql.execution;
22

33
import graphql.ErrorType;
4+
import graphql.ErrorTypeClassification;
45
import graphql.GraphQLError;
56
import graphql.Internal;
67
import graphql.language.SourceLocation;
@@ -25,7 +26,7 @@ public class AbsoluteGraphQLError implements GraphQLError {
2526
private final List<SourceLocation> locations;
2627
private final List<Object> absolutePath;
2728
private final String message;
28-
private final ErrorType errorType;
29+
private final ErrorTypeClassification errorType;
2930
private final Map<String, Object> extensions;
3031

3132
public AbsoluteGraphQLError(ExecutionStrategyParameters executionStrategyParameters, GraphQLError relativeError) {
@@ -68,7 +69,7 @@ public List<SourceLocation> getLocations() {
6869
}
6970

7071
@Override
71-
public ErrorType getErrorType() {
72+
public ErrorTypeClassification getErrorType() {
7273
return errorType;
7374
}
7475

0 commit comments

Comments
 (0)