1414/**
1515 * This helps you build {@link graphql.GraphQLError}s and also has a quick way to make a {@link graphql.execution.DataFetcherResult}s
1616 * from that error.
17+ *
18+ * @param <B> this base class allows you to derive new classes from this base error builder
1719 */
20+ @ SuppressWarnings ("unchecked" )
1821@ PublicApi
19- public class GraphqlErrorBuilder {
22+ public class GraphqlErrorBuilder < B extends GraphqlErrorBuilder <?>> {
2023
2124 private String message ;
2225 private List <Object > path ;
2326 private List <SourceLocation > locations = new ArrayList <>();
2427 private ErrorClassification errorType = ErrorType .DataFetchingException ;
2528 private Map <String , Object > extensions = null ;
2629
30+ public String getMessage () {
31+ return message ;
32+ }
33+
34+ public List <Object > getPath () {
35+ return path ;
36+ }
37+
38+ public List <SourceLocation > getLocations () {
39+ return locations ;
40+ }
41+
42+ public ErrorClassification getErrorType () {
43+ return errorType ;
44+ }
45+
46+ public Map <String , Object > getExtensions () {
47+ return extensions ;
48+ }
2749
2850 /**
2951 * @return a builder of {@link graphql.GraphQLError}s
3052 */
31- public static GraphqlErrorBuilder newError () {
32- return new GraphqlErrorBuilder ();
53+ public static GraphqlErrorBuilder <?> newError () {
54+ return new GraphqlErrorBuilder <> ();
3355 }
3456
3557 /**
@@ -40,52 +62,52 @@ public static GraphqlErrorBuilder newError() {
4062 *
4163 * @return a builder of {@link graphql.GraphQLError}s
4264 */
43- public static GraphqlErrorBuilder newError (DataFetchingEnvironment dataFetchingEnvironment ) {
44- return new GraphqlErrorBuilder ()
65+ public static GraphqlErrorBuilder <?> newError (DataFetchingEnvironment dataFetchingEnvironment ) {
66+ return new GraphqlErrorBuilder <> ()
4567 .location (dataFetchingEnvironment .getField ().getSourceLocation ())
4668 .path (dataFetchingEnvironment .getExecutionStepInfo ().getPath ());
4769 }
4870
49- private GraphqlErrorBuilder () {
71+ protected GraphqlErrorBuilder () {
5072 }
5173
52- public GraphqlErrorBuilder message (String message , Object ... formatArgs ) {
74+ public B message (String message , Object ... formatArgs ) {
5375 if (formatArgs == null || formatArgs .length == 0 ) {
5476 this .message = assertNotNull (message );
5577 } else {
5678 this .message = String .format (assertNotNull (message ), formatArgs );
5779 }
58- return this ;
80+ return ( B ) this ;
5981 }
6082
61- public GraphqlErrorBuilder locations (List <SourceLocation > locations ) {
83+ public B locations (List <SourceLocation > locations ) {
6284 this .locations .addAll (assertNotNull (locations ));
63- return this ;
85+ return ( B ) this ;
6486 }
6587
66- public GraphqlErrorBuilder location (SourceLocation location ) {
88+ public B location (SourceLocation location ) {
6789 this .locations .add (assertNotNull (location ));
68- return this ;
90+ return ( B ) this ;
6991 }
7092
71- public GraphqlErrorBuilder path (ResultPath path ) {
93+ public B path (ResultPath path ) {
7294 this .path = assertNotNull (path ).toList ();
73- return this ;
95+ return ( B ) this ;
7496 }
7597
76- public GraphqlErrorBuilder path (List <Object > path ) {
98+ public B path (List <Object > path ) {
7799 this .path = assertNotNull (path );
78- return this ;
100+ return ( B ) this ;
79101 }
80102
81- public GraphqlErrorBuilder errorType (ErrorClassification errorType ) {
103+ public B errorType (ErrorClassification errorType ) {
82104 this .errorType = assertNotNull (errorType );
83- return this ;
105+ return ( B ) this ;
84106 }
85107
86- public GraphqlErrorBuilder extensions (Map <String , Object > extensions ) {
108+ public B extensions (Map <String , Object > extensions ) {
87109 this .extensions = assertNotNull (extensions );
88- return this ;
110+ return ( B ) this ;
89111 }
90112
91113 /**
@@ -147,7 +169,7 @@ public String toString() {
147169 *
148170 * @return a new data fetcher result that contains the built error
149171 */
150- public DataFetcherResult toResult () {
172+ public DataFetcherResult <?> toResult () {
151173 return DataFetcherResult .newResult ()
152174 .error (build ())
153175 .build ();
0 commit comments