Skip to content

Commit 578985f

Browse files
authored
Introduce a GraphqlErrorException for Kotlin users and generally. (#1743)
* Introduce a GraphqlErrorException for Kotlin users and a as a general purpose exception that can be an error * PR feedback and now more generic
1 parent 21cdd1d commit 578985f

4 files changed

Lines changed: 130 additions & 150 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package graphql;
2+
3+
import graphql.language.SourceLocation;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
/**
10+
* A base class for graphql runtime exceptions that also implement {@link graphql.GraphQLError} and can be used
11+
* in a general sense direct or have specialisations made of it.
12+
* <p>
13+
* This is aimed amongst other reasons at Kotlin consumers due to https://github.com/graphql-java/graphql-java/issues/1690
14+
* as well as being a way to share common code.
15+
*/
16+
@PublicApi
17+
public class GraphqlErrorException extends GraphQLException implements GraphQLError {
18+
19+
private final List<SourceLocation> locations;
20+
private final Map<String, Object> extensions;
21+
private final List<Object> path;
22+
private final ErrorClassification errorClassification;
23+
24+
protected GraphqlErrorException(BuilderBase<?, ?> builder) {
25+
super(builder.message, builder.cause);
26+
this.locations = builder.sourceLocations;
27+
this.extensions = builder.extensions;
28+
this.path = builder.path;
29+
this.errorClassification = builder.errorClassification;
30+
}
31+
32+
@Override
33+
public List<SourceLocation> getLocations() {
34+
return locations;
35+
}
36+
37+
@Override
38+
public ErrorClassification getErrorType() {
39+
return errorClassification;
40+
}
41+
42+
@Override
43+
public List<Object> getPath() {
44+
return path;
45+
}
46+
47+
@Override
48+
public Map<String, Object> getExtensions() {
49+
return extensions;
50+
}
51+
52+
public static Builder newErrorException() {
53+
return new Builder();
54+
}
55+
56+
public static class Builder extends BuilderBase<Builder, GraphqlErrorException> {
57+
public GraphqlErrorException build() {
58+
return new GraphqlErrorException(this);
59+
}
60+
}
61+
62+
/**
63+
* A trait like base class that contains the properties that GraphqlErrorException handles and can
64+
* be used by other classes to derive their own builders.
65+
*
66+
* @param <T> the derived class
67+
* @param <B> the class to be built
68+
*/
69+
protected abstract static class BuilderBase<T extends BuilderBase<T, B>, B extends GraphqlErrorException> {
70+
protected String message;
71+
protected Throwable cause;
72+
protected ErrorClassification errorClassification = ErrorType.DataFetchingException;
73+
protected List<SourceLocation> sourceLocations;
74+
protected Map<String, Object> extensions;
75+
protected List<Object> path;
76+
77+
private T asDerivedType() {
78+
//noinspection unchecked
79+
return (T) this;
80+
}
81+
82+
public T message(String message) {
83+
this.message = message;
84+
return asDerivedType();
85+
}
86+
87+
public T cause(Throwable cause) {
88+
this.cause = cause;
89+
return asDerivedType();
90+
}
91+
92+
public T sourceLocation(SourceLocation sourceLocation) {
93+
return sourceLocations(sourceLocation == null ? null : Collections.singletonList(sourceLocation));
94+
}
95+
96+
public T sourceLocations(List<SourceLocation> sourceLocations) {
97+
this.sourceLocations = sourceLocations;
98+
return asDerivedType();
99+
}
100+
101+
public T errorClassification(ErrorClassification errorClassification) {
102+
this.errorClassification = errorClassification;
103+
return asDerivedType();
104+
}
105+
106+
public T path(List<Object> path) {
107+
this.path = path;
108+
return asDerivedType();
109+
}
110+
111+
public T extensions(Map<String, Object> extensions) {
112+
this.extensions = extensions;
113+
return asDerivedType();
114+
}
115+
116+
public abstract B build();
117+
}
118+
}

src/main/java/graphql/schema/CoercingParseLiteralException.java

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package graphql.schema;
22

33
import graphql.ErrorType;
4-
import graphql.GraphQLError;
5-
import graphql.GraphQLException;
4+
import graphql.GraphqlErrorException;
65
import graphql.PublicApi;
76
import graphql.language.SourceLocation;
87

9-
import java.util.Collections;
10-
import java.util.List;
11-
import java.util.Map;
12-
138
@PublicApi
14-
public class CoercingParseLiteralException extends GraphQLException implements GraphQLError {
15-
private final List<SourceLocation> sourceLocations;
16-
private final Map<String, Object> extensions;
9+
public class CoercingParseLiteralException extends GraphqlErrorException {
1710

1811
public CoercingParseLiteralException() {
1912
this(newCoercingParseLiteralException());
@@ -36,56 +29,19 @@ public CoercingParseLiteralException(Throwable cause) {
3629
}
3730

3831
private CoercingParseLiteralException(Builder builder) {
39-
super(builder.message, builder.cause);
40-
this.sourceLocations = builder.sourceLocations;
41-
this.extensions = builder.extensions;
42-
}
43-
44-
@Override
45-
public List<SourceLocation> getLocations() {
46-
return sourceLocations;
32+
super(builder);
4733
}
4834

4935
@Override
5036
public ErrorType getErrorType() {
5137
return ErrorType.ValidationError;
5238
}
5339

54-
@Override
55-
public Map<String, Object> getExtensions() {
56-
return extensions;
57-
}
58-
5940
public static Builder newCoercingParseLiteralException() {
6041
return new Builder();
6142
}
6243

63-
public static class Builder {
64-
private String message;
65-
private Throwable cause;
66-
private List<SourceLocation> sourceLocations;
67-
private Map<String, Object> extensions;
68-
69-
public Builder message(String message) {
70-
this.message = message;
71-
return this;
72-
}
73-
74-
public Builder cause(Throwable cause) {
75-
this.cause = cause;
76-
return this;
77-
}
78-
79-
public Builder sourceLocation(SourceLocation sourceLocation) {
80-
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
81-
return this;
82-
}
83-
84-
public Builder extensions(Map<String, Object> extensions) {
85-
this.extensions = extensions;
86-
return this;
87-
}
88-
44+
public static class Builder extends BuilderBase<Builder, CoercingParseLiteralException> {
8945
public CoercingParseLiteralException build() {
9046
return new CoercingParseLiteralException(this);
9147
}

src/main/java/graphql/schema/CoercingParseValueException.java

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package graphql.schema;
22

33
import graphql.ErrorType;
4-
import graphql.GraphQLError;
5-
import graphql.GraphQLException;
4+
import graphql.GraphqlErrorException;
65
import graphql.PublicApi;
76
import graphql.language.SourceLocation;
87

9-
import java.util.Collections;
10-
import java.util.List;
11-
import java.util.Map;
12-
138
@PublicApi
14-
public class CoercingParseValueException extends GraphQLException implements GraphQLError {
15-
private final List<SourceLocation> sourceLocations;
16-
private final Map<String, Object> extensions;
9+
public class CoercingParseValueException extends GraphqlErrorException {
1710

1811
public CoercingParseValueException() {
1912
this(newCoercingParseValueException());
@@ -36,61 +29,19 @@ public CoercingParseValueException(String message, Throwable cause, SourceLocati
3629
}
3730

3831
private CoercingParseValueException(Builder builder) {
39-
super(builder.message, builder.cause);
40-
this.sourceLocations = builder.sourceLocations;
41-
this.extensions = builder.extensions;
42-
}
43-
44-
@Override
45-
public List<SourceLocation> getLocations() {
46-
return sourceLocations;
32+
super(builder);
4733
}
4834

4935
@Override
5036
public ErrorType getErrorType() {
5137
return ErrorType.ValidationError;
5238
}
5339

54-
@Override
55-
public Map<String, Object> getExtensions() {
56-
return extensions;
57-
}
58-
5940
public static Builder newCoercingParseValueException() {
6041
return new Builder();
6142
}
6243

63-
public static class Builder {
64-
private String message;
65-
private Throwable cause;
66-
private List<SourceLocation> sourceLocations;
67-
private Map<String, Object> extensions;
68-
69-
public Builder message(String message) {
70-
this.message = message;
71-
return this;
72-
}
73-
74-
public Builder cause(Throwable cause) {
75-
this.cause = cause;
76-
return this;
77-
}
78-
79-
public Builder sourceLocation(SourceLocation sourceLocation) {
80-
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
81-
return this;
82-
}
83-
84-
public Builder sourceLocations(List<SourceLocation> sourceLocations) {
85-
this.sourceLocations = sourceLocations;
86-
return this;
87-
}
88-
89-
public Builder extensions(Map<String, Object> extensions) {
90-
this.extensions = extensions;
91-
return this;
92-
}
93-
44+
public static class Builder extends BuilderBase<Builder,CoercingParseValueException> {
9445
public CoercingParseValueException build() {
9546
return new CoercingParseValueException(this);
9647
}

src/main/java/graphql/schema/CoercingSerializeException.java

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@
22

33
import graphql.ErrorClassification;
44
import graphql.ErrorType;
5-
import graphql.GraphQLError;
6-
import graphql.GraphQLException;
5+
import graphql.GraphqlErrorException;
76
import graphql.PublicApi;
8-
import graphql.language.SourceLocation;
9-
10-
import java.util.Collections;
11-
import java.util.List;
12-
import java.util.Map;
137

148
@PublicApi
15-
public class CoercingSerializeException extends GraphQLException implements GraphQLError {
16-
private final List<SourceLocation> sourceLocations;
17-
private final Map<String, Object> extensions;
9+
public class CoercingSerializeException extends GraphqlErrorException {
1810

1911
public CoercingSerializeException() {
2012
this(newCoercingSerializeException());
@@ -33,56 +25,19 @@ public CoercingSerializeException(Throwable cause) {
3325
}
3426

3527
private CoercingSerializeException(Builder builder) {
36-
super(builder.message, builder.cause);
37-
this.sourceLocations = builder.sourceLocations;
38-
this.extensions = builder.extensions;
39-
}
40-
41-
@Override
42-
public List<SourceLocation> getLocations() {
43-
return sourceLocations;
28+
super(builder);
4429
}
4530

4631
@Override
4732
public ErrorClassification getErrorType() {
4833
return ErrorType.DataFetchingException;
4934
}
5035

51-
@Override
52-
public Map<String, Object> getExtensions() {
53-
return extensions;
54-
}
55-
5636
public static Builder newCoercingSerializeException() {
5737
return new Builder();
5838
}
5939

60-
public static class Builder {
61-
private String message;
62-
private Throwable cause;
63-
private Map<String, Object> extensions;
64-
private List<SourceLocation> sourceLocations;
65-
66-
public Builder message(String message) {
67-
this.message = message;
68-
return this;
69-
}
70-
71-
public Builder cause(Throwable cause) {
72-
this.cause = cause;
73-
return this;
74-
}
75-
76-
public Builder sourceLocation(SourceLocation sourceLocation) {
77-
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
78-
return this;
79-
}
80-
81-
public Builder extensions(Map<String, Object> extensions) {
82-
this.extensions = extensions;
83-
return this;
84-
}
85-
40+
public static class Builder extends BuilderBase<Builder, CoercingSerializeException> {
8641
public CoercingSerializeException build() {
8742
return new CoercingSerializeException(this);
8843
}

0 commit comments

Comments
 (0)